Welcome Guest, Not a member yet? Register   Sign In
Submit button doesn't go to intended route
#1

[eluser]ethereal1m[/eluser]
Dear all,
I'm following CI tutorial on creating "News" page. I got problem when submit button in creating news is pushed, it doesn't bring execute create page controller but instead it executes view page controller. I suspect the problem lies on the router.

Here is the route configuration:
Code:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
if submit button pressed, it's supposed to go to news/create, but it doesn't. I suppose the form_open function take care of that, right?

The create news view:
Code:
<h2>Create a news item</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('news/create') ?&gt;
    
<label for="title">Title</label>
&lt;input type="input" name="title" /&gt;&lt;br />

<label for="text">Text</label>
&lt;textarea name="text"&gt;&lt;/textarea><br />

&lt;input type="submit" name="submit" value="Create news item" /&gt;

&lt;/form&gt;

Here is the controller, when submit is pressed, it supposed to go to "create" function, but instead it goes to "view" function.
Code:
class News extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->load->model('news_model');
}

public function index()
{
  $data['news'] = $this->news_model->get_news();
    $data['title'] = 'News archive';

   $this->load->view('templates/header', $data);
   $this->load->view('news/index', $data);
   $this->load->view('templates/footer');
}

public function view($slug)
{
  $data['news'] = $this->news_model->get_news($slug);
      
      if (empty($data['news_item']))
   {
    show_404();
   }

   $data['title'] = $data['news_item']['title'];

   $this->load->view('templates/header', $data);
   $this->load->view('news/view', $data);
   $this->load->view('templates/footer');
}
  
  public function create()
  {
   $this->load->helper('form');
   $this->load->library('form_validation');

   $data['title'] = 'Create a news item';

   $this->form_validation->set_rules('title', 'Title', 'required');
   $this->form_validation->set_rules('text', 'text', 'required');

   if ($this->form_validation->run() === FALSE)
   {
    $this->load->view('templates/header', $data);
    $this->load->view('news/create');
    $this->load->view('templates/footer');
  
   }
   else
   {
    $this->news_model->set_news();
    $this->load->view('news/success');
   }
  }
}

What did I miss?

regards,
ethereal1m
#2

[eluser]ethereal1m[/eluser]
also in $_SERVER['REQUEST_URI'] variable is set to double URL. For some reason the submit button makes URL request into double URL such as:
Code:
http://localhost/ci/index.php/news/ci/index.php/news/create
This is causing the route direct to undesired location.

What makes it behaving like that?
#3

[eluser]solid9[/eluser]
First of all did you set the config setting properly below?

$config['base_url'] = 'http://samplesite.com/';

And used proper .htaccess ?
#4

[eluser]ethereal1m[/eluser]
my config:
Code:
$config['base_url'] = 'localhost/ci';

and I'm using proper .htaccess.


looks like form_open from view is causing the problem
Code:
&lt;?php echo form_open('news/create') ?&gt;

the problem is gone when I changed the above code with
Code:
&lt;form method="post" action="http://localhost/ci/index.php/news/create"&gt;

I'm using 2.0.3 since 2.1.0 has problem on OCI8 driver....

is this a bug in form_open procedure?


#5

[eluser]solid9[/eluser]
try changing from this
Code:
&lt;input type="submit" name="submit" value="Create news item" /&gt;

to this,
Code:
&lt;?php echo form_submit('submit', 'Create news item'); ?&gt;

#6

[eluser]solid9[/eluser]
also change from this,
Code:
&lt;/form&gt;

to this,
Code:
&lt;?php
echo form_close();
?&gt;

#7

[eluser]InsiteFX[/eluser]
You also need to add an ending slash on to your base_url!
#8

[eluser]ethereal1m[/eluser]
nope, I changed my view to
Code:
<h2>Create a news item</h2>

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('news/create') ?&gt;

<label for="title">Title</label>
&lt;input type="input" name="title" /&gt;&lt;br />

<label for="text">Text</label>
&lt;textarea name="text"&gt;&lt;/textarea><br />

  &lt;?php echo form_submit('submit', 'Create news item'); ?&gt;
  

&lt;?php echo form_close(); ?&gt;
and also my base_url in config file doesn't fix the problem..
#9

[eluser]ethereal1m[/eluser]
With the above code and change this
Code:
&lt;?php echo form_open('news/create') ?&gt;

to

Code:
&lt;form method="post" action="http://localhost/ci/index.php/news/create"&gt;

fixes this....
#10

[eluser]solid9[/eluser]
That's fine, but you are not following CodeIgniter standard.

The advantage of using the codes below,
Code:
&lt;?php echo form_open('news/create') ?&gt;

Is portability.




Theme © iAndrew 2016 - Forum software by © MyBB