一种在POST方法中确定表单ID的方法

时间:2018-06-01 12:08:14

标签: php html

我想知道是否能以任何方式确定在PHP上发布的表单的ID?

<form id="ES1A" action="enroll.php" method="post">
<input type="checkbox"/>
<button type="submit" class="btn btn-primary">Next Step</button>
</form>

在enroll.php中

if(isset($_POST['ES1A']))
{
  //Code after checking that the form that was submitted indeed has the ID of ES1A
}

P.S:我不太确定如何在PHP上做这个。提前谢谢

3 个答案:

答案 0 :(得分:1)

Post不使用元素的ID,而是使用名称,因此您可以使用;而不是当前的表单;

<form name="ES1A" action="enroll.php" method="post">
    <input type="checkbox"/>
    <input name="formid" value="ES1A" /><!-- This holds your form id so you can use this -->
    <button type="submit" class="btn btn-primary">Next Step</button>
</form>

在PHP中;

if (isset($_POST['ES1A']) // Unsure if form itself will be posted with the submit
{
    // This is set as it uses the name of the element
    $formid = $_POST['formid']; // Get the ID of the form from the element passed in
}

答案 1 :(得分:0)

如果表单的名称属性不起作用,您始终可以为按钮设置名称:

<button name="ES1A" type="submit" class="btn btn-primary">Next Step</button>

或者:

<input name="ES1A" type="submit" class="btn btn-primary" value="Next Step">

PHP部分应与您已有的相同:

if(isset($_POST['ES1A']))
{
  //Code after checking that the form that was submitted indeed has the ID of ES1A
}

答案 2 :(得分:0)

另一种方法是使用隐藏输入

 <input name="ES1A" value="formid" type="hidden" />