如何知道点击了哪个提交按钮?

时间:2012-08-17 19:37:54

标签: forms cakephp

我有两个按钮,

<?php echo (($active_players >= 25 && !$active_players_check_bypass) ? $html->submit('Submit Registration', array('onclick'=>'return window.confirm("You have reached your maximum allowable players per team. In order to register this player, you must pay the $25.00 required overage fee in order to continue. Do you wish to continue with the registration?")')) : $html->submit('Submit Registration'));
  echo $html->submit('Register Player using Registration Credits', array('id'=>'submitregistrationusingcredits')); // this will use the credits of the team
?>

第一个按钮是Submit Registration,另一个是Register Player using Registration Credits,现在有办法知道我点击了哪个按钮吗?我正在使用CakePHP并且我对此非常新,有没有办法知道我点击了哪个按钮,以便在我的控制器中验证表单时,我将能够告诉我点击了什么按钮并拥有属于的进程每个按钮?感谢。

2 个答案:

答案 0 :(得分:1)

在每个提交按钮上设置名称属性。

echo (($active_players >= 25 && !$active_players_check_bypass) ? $html->submit('Submit Registration', array('name'=>'submit1a', 'onclick'=>'return window.confirm("You have reached your maximum allowable players per team. In order to register this player, you must pay the $25.00 required overage fee in order to continue. Do you wish to continue with the registration?")')) : $html->submit('Submit Registration', array('name'=>'submit1b'));
echo $html->submit('Register Player using Registration Credits', array('name'=>'submit2', 'id'=>'submitregistrationusingcredits')); // this will use the credits of the team

现在,在即将到来的页面上,您可以看到是否使用此代码点击了第一个按钮:

if ( isset($_REQUEST['submit1a']) ) ... // some code
// or, if the first button has the second possibility
if ( isset($_REQUEST['submit1b']) ) ... // some code

查看是否点击了第二个按钮:

if ( isset($_REQUEST['submit2']) ) ... // some code

答案 1 :(得分:1)

我并不熟悉CakePHP的HTMl或Form Helper(或者你在那里使用的任何东西)。但是我想如果你在第二个数组参数中添加一个name参数:

$html->submit('Register Player using Registration Credits', array('name' => 'usingCredits', 'id' => 'submitregistrationusingcredits'));

为两个提交按钮执行此操作。然后,当您处理该请求时,您可以编写一些如下所示的代码:

if (isset($_POST['usingCredits'])) 
{
    // handle submission using credits
} 
else
{
    // handle another submission method
}

您可能希望为两者指定名称并检查是否已设置每个名称。但这就是想法。