Codeigniter通过单击按钮更改表单操作 [英] Codeigniter change form action by clicking buttons

查看:103
本文介绍了Codeigniter通过单击按钮更改表单操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格中有2个提交按钮,我试图让每个按钮都转到不同的控制器方法。我试图避免使用多种形式。

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.

我目前正在使用

<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?

推荐答案

通常我想通过给button / input标签命名attr来处理多个提交。通过这种方式,您可以通过检查提交的按钮来提交和调用同一控制器中的一个功能/操作。
eg:

Usually 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>

然后在你的控制器中,会有一个名为process的函数来做这件事:

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
  }   
}

希望这会有所帮助。

这篇关于Codeigniter通过单击按钮更改表单操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆