如何检测 Zend Framework 表单上按下了哪个提交按钮?

发布于 2024-11-01 15:06:24 字数 488 浏览 4 评论 0原文

我有一个 Zend Framework 表单,它有两个提交按钮,

$changes = new Zend_Form_Element_Submit('save_changes');
$changes->setLabel('Save Changes');

$delete = new Zend_Form_Element_Submit('delete');
$delete->setLabel('Delete');

它呈现 HTML 如下:

<input type="submit" name="save_changes" id="user_save_changes" value="Save Changes" >
<input type="submit" name="delete" id="user_delete" value="Delete" >

在控制器中,如何确定用户按下了哪个按钮?

I have a Zend Framework form that has two submit buttons

$changes = new Zend_Form_Element_Submit('save_changes');
$changes->setLabel('Save Changes');

$delete = new Zend_Form_Element_Submit('delete');
$delete->setLabel('Delete');

Which renders HTML like such:

<input type="submit" name="save_changes" id="user_save_changes" value="Save Changes" >
<input type="submit" name="delete" id="user_delete" value="Delete" >

In the controller, how do I determine which button the user pressed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怼怹恏 2024-11-08 15:06:24

在您的情况下,您应该能够检查

if(isset($_POST['save_changes'])
// or
if(isset($_POST['delete'])

因为只有单击的按钮的值才会被提交。

通常,您为两个按钮指定相同的名称(例如操作),然后设置
您想要执行的操作的价值。不幸的是,这效果不太好
与IE。请查看此页面,了解有关多个问题的不同解决方案的更多信息
提交按钮。

In your case you should just be able to check

if(isset($_POST['save_changes'])
// or
if(isset($_POST['delete'])

Since only the value of the clicked button will be submitted.

Usually you give both buttons the same name (e.g. action) and then set the
value to the action you want to perform. Unfortunately that doesn't work very well
with IE. Check this page for more information about different solutions for multiple
submit buttons.

我是有多爱你 2024-11-08 15:06:24

由于您使用的是 Zend,我会推荐一种更接近 Zend 的方法。

您可以直接通过元素的名称来调用元素,Zend 有一个用于表单按钮(按钮、重置、提交)的方法,称为 isChecked()

在你的代码中它将是:

if ($form->save_changes->isChecked()) {
    // Saving ...
else if ($form->delete->isChecked()) {
    // Removing ...

Since you are using Zend I would recommend a more Zend-ish approach.

You can call elements directly by theirs names and Zend has a method for form buttons (buttons, reset, submits) called isChecked().

in your code it would be:

if ($form->save_changes->isChecked()) {
    // Saving ...
else if ($form->delete->isChecked()) {
    // Removing ...
终陌 2024-11-08 15:06:24

实际上你可以通过以下方式得到这个:

if($this->getRequest()->getPost('save_changes'){
//Code here
}

if($this->getRequest()->getPost('delete'){
//Code here
}

我之所以做了两个 if 条件,是因为你不能做 if else ,因为即使你没有单击任何提交按钮,你也会加载该页面,另一个条件也会被执行。

例子:

if($this->getRequest()->getPost('save_changes'){
  //once you load this will become true because you didn't click this
}else{
  //once you load this page this will become true because you didn't click the save_changes submit button
}

真实的故事。

Actually you can get this by:

if($this->getRequest()->getPost('save_changes'){
//Code here
}

if($this->getRequest()->getPost('delete'){
//Code here
}

The reason I made two if condition because you can't do if else because one you load that page even though you didn't click any submit button, the other condition will be executed.

Example:

if($this->getRequest()->getPost('save_changes'){
  //once you load this will become true because you didn't click this
}else{
  //once you load this page this will become true because you didn't click the save_changes submit button
}

True story.

白云不回头 2024-11-08 15:06:24
$data = $this->getRequest()->getPost();
if (array_key_exists('save_changes', $data)) {
..
} else if (array_key_exists('delete', $data)) {
..
}
$data = $this->getRequest()->getPost();
if (array_key_exists('save_changes', $data)) {
..
} else if (array_key_exists('delete', $data)) {
..
}
够运 2024-11-08 15:06:24
 $formData = $this->getRequest()->getPost(); 

if($formData['submit']=='save_changes'){ // echo "save chanes" ; }
if($formData['submit']=='delete'){ // echo "delete";}
 $formData = $this->getRequest()->getPost(); 

if($formData['submit']=='save_changes'){ // echo "save chanes" ; }
if($formData['submit']=='delete'){ // echo "delete";}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文