如何检测 Zend Framework 表单上按下了哪个提交按钮?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的情况下,您应该能够检查
因为只有单击的按钮的值才会被提交。
通常,您为两个按钮指定相同的名称(例如操作),然后设置
您想要执行的操作的价值。不幸的是,这效果不太好
与IE。请查看此页面,了解有关多个问题的不同解决方案的更多信息
提交按钮。
In your case you should just be able to check
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.
由于您使用的是 Zend,我会推荐一种更接近 Zend 的方法。
您可以直接通过元素的名称来调用元素,Zend 有一个用于表单按钮(按钮、重置、提交)的方法,称为
isChecked()
。在你的代码中它将是:
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 条件,是因为你不能做 if else ,因为即使你没有单击任何提交按钮,你也会加载该页面,另一个条件也会被执行。
例子:
真实的故事。
Actually you can get this by:
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:
True story.