[英]getting the number of arguments sent
我的php文件将采用以下两种形式之一。 一个包含2个输入,另一个包含1个输入。 有没有一种方法可以根据发送的参数数量来获取提交的两者之一?
<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"
?>
我建议您为此使用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>
而且您的php代码将如下所示:
if ($_POST['formName'] === 'formA') { handleFormA(); }
if ($_POST['formName'] === 'formB') { handleFormB(); }
但是,如果您希望依靠参数数量,可以执行以下操作:
if (count($_POST[]) === 3) { handleFormA(); }
if (count($_POST[]) === 1) { handleFormB(); }
这是决定已过帐哪个表格的非常危险的方式。 如果在3个月内您请求将另一个字段添加到包含2个字段的表单中,该怎么办? 然后它们都具有3个字段。
您需要做的就是给每个表单上的按钮一个唯一的名称,就像这样
<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>
现在在您的PHP中,您可以检查按钮的存在,您将知道您正在处理哪种形式
<?php
if ( isset($_POST['submitbutton1']) ) {
// I am processing form 1
}
if ( isset($_POST['submitbutton2']) ) {
// I am processing form 2
}
您也可以尝试:
<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>
您不需要检查隐藏的输入值,可以按如下输入名称进行检查:
if( array_key_exists( 'formAinputname', $_POST ) )
{
handleFormA();
}
if( array_key_exists( 'formBinputname', $_POST ) )
{
handleFormB();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.