[英]getting the number of arguments sent
My php file will recive one of two forms. 我的php文件将采用以下两种形式之一。 One contains 2 inputs, the other contains one.
一个包含2个输入,另一个包含1个输入。 Is there a way to get which one of the two was submited based on the number of arguments sent?
有没有一种方法可以根据发送的参数数量来获取提交的两者之一?
<form action="a.php" method="post">
<input type="text" name="firstA" />
<input type="text" name="secondA" />
<input type="submit" name="submitbutton" />
</form>
<form action="a.php" method="post">
<input type="text" name="B" />
<input type="submit" name="submitbutton" />
</form>
<?php
$number = "number of arguments"
?>
I'd rather advice you to use hidden
input for this purposes, like: 我建议您为此使用
hidden
输入,例如:
<form action="a.php" method="post">
<input type="text" name="firstA" />
<input type="text" name="secondA" />
<input type="submit" name="submitbutton" />
<input type="hidden" name="formName" value="formA" />
</form>
<form action="a.php" method="post">
<input type="text" name="B" />
<input type="submit" name="submitbutton" />
<input type="hidden" name="formName" value="formB" />
</form>
And your php code will look like this: 而且您的php代码将如下所示:
if ($_POST['formName'] === 'formA') { handleFormA(); }
if ($_POST['formName'] === 'formB') { handleFormB(); }
But if you wish to rely on count of arguments you can to do something like: 但是,如果您希望依靠参数数量,可以执行以下操作:
if (count($_POST[]) === 3) { handleFormA(); }
if (count($_POST[]) === 1) { handleFormB(); }
That is a very dangerous way of deciding which form has been posted. 这是决定已过帐哪个表格的非常危险的方式。 What if in 3 months you geta request to add another field to the form with 2 fields.
如果在3个月内您请求将另一个字段添加到包含2个字段的表单中,该怎么办? Then they both have 3 fields.
然后它们都具有3个字段。
All you need to do is give the button on each form a unique name like this 您需要做的就是给每个表单上的按钮一个唯一的名称,就像这样
<form action="a.php" method="post">
<input type="text" name="firstA" />
<input type="text" name="secondA" />
<input type="submit" name="submitbutton1" />
</form>
<form action="a.php" method="post">
<input type="text" name="B" />
<input type="submit" name="submitbutton2" />
</form>
Now in your PHP you can check for the existance of the button and you will know which form you are processing 现在在您的PHP中,您可以检查按钮的存在,您将知道您正在处理哪种形式
<?php
if ( isset($_POST['submitbutton1']) ) {
// I am processing form 1
}
if ( isset($_POST['submitbutton2']) ) {
// I am processing form 2
}
you can also try: 您也可以尝试:
<form action="a.php" method="post">
<input type="text" name="firstA" />
<input type="text" name="secondA" />
<input type="submit" name="submitbutton" />
<input type="hidden" name="formAinputname" value="formA" />
</form>
<form action="a.php" method="post">
<input type="text" name="B" />
<input type="submit" name="submitbutton" />
<input type="hidden" name="formBinputname" value="formB" />
</form>
you not need check hidden input value you can check by input name like below: 您不需要检查隐藏的输入值,可以按如下输入名称进行检查:
if( array_key_exists( 'formAinputname', $_POST ) )
{
handleFormA();
}
if( array_key_exists( 'formBinputname', $_POST ) )
{
handleFormB();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.