admin管理员组文章数量:1278857
I have 2 submit buttons in my form and I'm trying to make each one go to a different controller method. I'm trying not avoid the use of multiple forms.
I'm currently using
<button type="submit" onclick="javascript: form.action='restore'">Restore</button>
<button type="submit" onclick="javascript: form.action='delete'">Delete</button>
...which works but I'm not sure if that's the best method. Any thoughts?
I have 2 submit buttons in my form and I'm trying to make each one go to a different controller method. I'm trying not avoid the use of multiple forms.
I'm currently using
<button type="submit" onclick="javascript: form.action='restore'">Restore</button>
<button type="submit" onclick="javascript: form.action='delete'">Delete</button>
...which works but I'm not sure if that's the best method. Any thoughts?
Share Improve this question edited Jul 3, 2012 at 23:58 CyberJunkie asked Jul 3, 2012 at 22:44 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges3 Answers
Reset to default 5Usually I'd like to handle the multiple submits by giving button/input tag a name attr. In this way you can submit and call to one function/action in one same controller, just by checking which button was submitted. e.g:
<form id="your_form" action="your_controller/process" method="post">
<input type="submit" name="restore" id="restore" value="Restore" />
<input type="submit" name="delete" id="delete" value="Delete" />
</form>
Then in your controller, there will be a function called "process" doing this:
function process(){
if(isset($_POST["restore"])) {
//do your restore code
}
if(isset($_POST["delete"])){
//do your delete code
}
}
Hope this would help.
Best to do is with javascript, after all it's being done after page is rendered. This is an alternative solution (sorry it's jQuery I'm not very good at vanilla JS).
View:
<div id="submit-buttons" action="<?php echo site_url('controller/postmethod1'); ?>">Submit 1</div>
<div id="submit-buttons" action="<?php echo site_url('controller/postmethod2'); ?>">Submit 2</div>
JS:
$(function(){
$('.submit-buttons').click(function(){
$('form').attr('action',$(this).attr('action')).submit();
return false;
});
});
So when after buttons were clicked, it will update the form action parameter from the action attribute of the submit button, and then submit the form right after that.
P.s: I didn't test it, but it should work.
What about this?
<?php echo form_open('controller/method',array('id'=>'my_form')); ?>
<!--Form-->
<input type="submit" value="Submit" />
<?php echo form_close();?>
Hope it helps
本文标签: phpCodeigniter change form action by clicking buttonsStack Overflow
版权声明:本文标题:php - Codeigniter change form action by clicking buttons - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741284695a2370201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论