php Codeigniter 按钮重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20581600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter button redirect
提问by user3051523
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.
我只是想问一下,因为我正在使用 Codeigniter 并且我在主页上有一个按钮,如果点击,它会将页面重定向到注册页面。
<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.
问题是当我尝试单击按钮时没有任何反应。
回答by CodeColorist
CodeIgniter's redirect() function means to make a HTTP 302response, which only works before content being outputted. It's designed for controller and should never appear in your views.
CodeIgniter 的redirect() 函数意味着做一个HTTP 302响应,它只在内容被输出之前起作用。它是为控制器设计的,不应出现在您的视图中。
Navigation in browser-side can be implemented by Javascript:
浏览器端的导航可以通过Javascript实现:
<button onclick="location.href='<?php echo base_url();?>register/index'">Register</button>
回答by Hugo B.
If you want to use the CI form helper you have use it like this:
如果你想使用 CI 表单助手,你可以像这样使用它:
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:
如果你想让你的表单包含一些额外的数据,比如 JavaScript,你可以在第三个参数中将它作为字符串传递:
$js = 'onClick="some_function()"';
echo form_button('mybutton', 'Click Me', $js);
And make sure that the helper is loaded:
并确保加载了帮助程序:
$this->load->helper('form');
回答by Nouphal.M
You could write a common javascript function for your project if you need and can call it like below :
如果需要,您可以为您的项目编写一个通用的 javascript 函数,并且可以像下面这样调用它:
<button onClick="return redirect('<?php echo base_url();?>register/index');">Register</button>
and javascript function is as below:
和javascript函数如下:
<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.
请记住返回 false 以便忽略按钮单击的默认事件。

