[英]Two submit buttons, one form (Yii)
I have two CHtml::submitButton() in my form, namely "Accept" and "Reject". 我的表单中有两个CHtml :: submitButton(),分别是“ Accept”和“ Reject”。 Each of the buttons has a specific ID ... When any of the two are pressed, i would like to trigger an action in my controller.
每个按钮都有一个特定的ID ...当按下两个按钮中的任何一个时,我想在控制器中触发一个动作。 Below are the code snippets.
以下是代码片段。
========================================================= ================================================== =======
View 视图
<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>
<?php echo CHtml::submitButton('Accept', array('id' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject')); ?>
<?php echo CHtml::endForm(); ?>
======================================================== ================================================== ======
Controller 控制者
public function actionAcceptUserRegistration() {
$value = $_GET['id'];
if($value == "accept"){
//will do something here
}
if($value == "reject"){
//will do something here.
}
}
====================================================================== ================================================== ====================
When i implement it this way. 当我以这种方式实现它。 the get value in the controller side is empty.
控制器端的获取值为空。 What I'm I doing wrong?
我做错了什么? Or is there any other way around this?
还是有其他解决方法?
You forgot to give the submit button a name (unless your framework does some magic which we cannot know about - you should really post the generated HTML code!). 您忘了给Submit按钮起个名字了(除非您的框架做了一些我们不知道的魔术-您应该真正发布生成的HTML代码!)。
If you do so, the value in $_GET['buttonname']
will be its value (not its id), ie Accept
or Reject
. 如果这样做,
$_GET['buttonname']
中的值将是它的值 (而不是其id),即Accept
或Reject
。 This becomes pretty messy as soon as you get into i18n, so you might want to use different names for the buttons and then check isset($_GET['buttonname1'])
and isset($_GET['buttonname2'])
一旦进入i18n,这将变得非常混乱,因此您可能想为按钮使用不同的名称 ,然后检查
isset($_GET['buttonname1'])
和isset($_GET['buttonname2'])
Here is a code example for what you are trying to achive: 这是您要实现的代码示例:
VIEW: 视图:
<?php
echo CHtml::beginForm('mycontrollerName/AcceptUserRegistration','get');
?>
<?php echo CHtml::submitButton('Accept', array('id' => 'accept', 'name' => 'accept')); ?>
<? echo ' '; ?>
<?php echo CHtml::submitButton('Reject', array('id' => 'reject', 'name' => 'reject')); ?>
<?php echo CHtml::endForm(); ?>
Note: I added the name parameter. 注意:我添加了名称参数。 As name is not displayed to the user, you can use accept instead of Accept.
由于名称不会显示给用户,因此可以使用accept代替Accept。
CONTROLLER: 控制器:
public function actionAcceptUserRegistration() {
if(isset($_GET['accept'])) {
//accepted
} else if(isset($_GET['reject'])) {
//rejected
}
//I recommend using POST method instead of GET for posting the form.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.