I just want to ask because I'm working on Codeigniter and I'm have a button in home page wherein if click, it will page redirect to registration page.
<input type='button' onClick='<?php redirect(base_url() . 'register/index'); ?>' />
I also tried the form helper.
<body>
<?php
$data = array('register' => array('name' => 'register',
'id' => 'register',
'value' => 'TRUE',
'type' => 'button',
'content' => 'Register',
'onClick' => redirect(base_url() . 'register/index')
);
?>
<table border="1">
<tr>
<td><?php echo form_button(element('register', $data, )); ?></td>
<td></td>
</tr>
</table>
</body>
The problem is nothing happens when I try to click the button.
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.