我有多个表单与一个单一的动作,一个php页面得到所有的表单调用。
如何区分哪个表单被发送到php页面?
为每个表单使用不同的唯一input type="hidden"
<input type="hidden" name="form_id" value="1">
<input type="hidden" name="form_id" value="2">
PHP: $myform = $_POST["form_id"];
你也可以使用提交按钮,但注意"value"参数是显示给用户的,所以你不能修改它(假设你想在每个按钮上显示相同的文本)。
<input type="submit" name="action" value="the user saw this">
PHP: $_POST["action"] // -> "the user saw this";
为每个字段添加一个隐藏字段(action
或类似的),然后检查它
<form id="num1">
<input type="hidden" name="action" value="first_action" />
</form>
…还有支票:
<?php
if(!empty($_REQUEST['action']) {
switch($_REQUEST['action']) {
case 'first_action':
// first action code
break;
}
}
?>
给每个提交按钮另起一个名字,或者在每个表单中使用不同的值
你也可以从提交按钮本身检测到这一点,如果提交有不同的值,如:
在每个表单的提交按钮上,使用不同的名称。比如:
<input type="submit" name="submit_1" value="Submit" />
<input type="submit" name="submit_2" value="Submit" />
<input type="submit" name="submit_3" value="Submit" />
...
在你的PHP中,你有:
$_POST["submit_1"]
$_POST["submit_2"]
$_POST["submit_3"]