Codeigniter button redirect Codeigniter button redirect codeigniter codeigniter

Codeigniter button redirect


CodeIgniter's redirect() function means to make a HTTP 302 response, which only works before content being outputted. It's designed for controller and should never appear in your views.

Navigation in browser-side can be implemented by Javascript:

<button onclick="location.href='<?php echo base_url();?>register/index'">Register</button>


If you want to use the CI form helper you have use it like this:

echo form_button('name','content');// Would produce// <button name="name" type="button">Content</button>

Or you can pass an associative array containing any data you wish your form to contain:

$data = array(    'name' => 'button',    'id' => 'button',    'value' => 'true',    'type' => 'reset',    'content' => 'Reset');echo form_button($data);// Would produce:// <button name="button" id="button" value="true" type="reset">Reset</button>

If you would like your form to contain some additional data, like JavaScript, you can pass it as a string in the third parameter:

$js = 'onClick="some_function()"';echo form_button('mybutton', 'Click Me', $js); 

And make sure that the helper is loaded:

$this->load->helper('form');


You could write a common javascript function for your project if you need and can call it like below :

<button onClick="return redirect('<?php echo base_url();?>register/index');">Register</button>

and javascript function is as below:

<script>function redirect(url){window.location.href = url;return false;}</script>

OR you could simply do the following

<button onClick="window.location.href = '<?php echo base_url();?>register/index';return false;">Register</button>

Please remember to return false so that the default event of button click is ignored.