HTML表单如何在POST标题中标识自己?

时间:2023-01-18 16:58:29

The php page is called page.php; this pages has 2 submit forms on it: form1 and form2. When one of the form's submit button is pressed what in the HTML header with identify which form was submitted?

php页面名为page.php;这个页面上有2个提交表单:form1和form2。当其中一个表单的提交按钮被按下时,HTML标题中的内容是否标识了提交的表单?

4 个解决方案

#1


I don't believe that it does post any identification. The easiest way to have your code know which form posted is to put a hidden field in each form identifying the form like this:

我不相信它会发布任何身份证明。让代码知道发布哪个表单的最简单方法是在每个表单中放置一个隐藏字段来标识表单,如下所示:

<form id="form1">
  <input type="hidden" name="formName" value="form1"/>
  <input type="submit" value="submit" />
</form>
<form id="form2">
  <input type="hidden" name="formName" value="form2"/>
  <input type="submit" value="submit" />
</form>

#2


As mentioned in che's comment on Jacob's answer:

正如切尔对雅各布答案的评论所述:

<form id="form1">
  <input type="submit" value="submit" name="form1" />
</form>
<form id="form2">
  <input type="submit" value="submit" name="form2" />
</form>

And then in your form handling script:

然后在您的表单处理脚本中:

if(isset($_POST['form1']){
    // do stuff
}

This is what I use when not submitting forms via ajax.

这是我在不通过ajax提交表单时使用的。

#3


What about the action attribute of the form tag?

form标签的action属性怎么样?

I would have guesssed that you could specify a different action attribute (each with a different URI value) in the different form instances.

我猜你可以在不同的表单实例中指定不同的action属性(每个属性具有不同的URI值)。

Also, +1 to adding a name attribute to the submit buttons: if you do then the name of the "successful" (i.e. clicked) submit button will be added to the string of names-plus-values which the form returns to the server.

此外,+1为提交按钮添加名称属性:如果您这样做,那么“成功”(即点击)提交按钮的名称将被添加到名称 - 字符串的字符串,表单返回到服务器。

#4


the way "rpflo" uses does not identify forms. the $_POST['form1'] here corresponds to the input with name="form1", not to the form with id="form1".

“rpflo”使用的方式不能识别表格。这里的$ _POST ['form1']对应于name =“form1”的输入,而不是id =“form1”的表单。

there are IMHO two reasonable ways to identify two forms on one page. first is via 'action' attribute by adding a GET variable in to it, like action="mypage.php?form_id=1". and second way, which is imho more practical, is to name all inputs like an array. for example:

恕我直言有两种合理的方法可以在一个页面上识别两种形式。首先是通过'action'属性向其添加一个GET变量,如action =“mypage.php?form_id = 1”。第二种方式,即更实用,就是将所有输入命名为数组。例如:

<form>
  <input name="form1[first_name]" />
  <input name="form1[last_name]" />
</form>
<form>
  <input name="form2[first_name]" />
  <input name="form2[last_name]" />
</form>

then you have $_POST['form1']['first_name'] and so on..

然后你有$ _POST ['form1'] ['first_name']等等..

#1


I don't believe that it does post any identification. The easiest way to have your code know which form posted is to put a hidden field in each form identifying the form like this:

我不相信它会发布任何身份证明。让代码知道发布哪个表单的最简单方法是在每个表单中放置一个隐藏字段来标识表单,如下所示:

<form id="form1">
  <input type="hidden" name="formName" value="form1"/>
  <input type="submit" value="submit" />
</form>
<form id="form2">
  <input type="hidden" name="formName" value="form2"/>
  <input type="submit" value="submit" />
</form>

#2


As mentioned in che's comment on Jacob's answer:

正如切尔对雅各布答案的评论所述:

<form id="form1">
  <input type="submit" value="submit" name="form1" />
</form>
<form id="form2">
  <input type="submit" value="submit" name="form2" />
</form>

And then in your form handling script:

然后在您的表单处理脚本中:

if(isset($_POST['form1']){
    // do stuff
}

This is what I use when not submitting forms via ajax.

这是我在不通过ajax提交表单时使用的。

#3


What about the action attribute of the form tag?

form标签的action属性怎么样?

I would have guesssed that you could specify a different action attribute (each with a different URI value) in the different form instances.

我猜你可以在不同的表单实例中指定不同的action属性(每个属性具有不同的URI值)。

Also, +1 to adding a name attribute to the submit buttons: if you do then the name of the "successful" (i.e. clicked) submit button will be added to the string of names-plus-values which the form returns to the server.

此外,+1为提交按钮添加名称属性:如果您这样做,那么“成功”(即点击)提交按钮的名称将被添加到名称 - 字符串的字符串,表单返回到服务器。

#4


the way "rpflo" uses does not identify forms. the $_POST['form1'] here corresponds to the input with name="form1", not to the form with id="form1".

“rpflo”使用的方式不能识别表格。这里的$ _POST ['form1']对应于name =“form1”的输入,而不是id =“form1”的表单。

there are IMHO two reasonable ways to identify two forms on one page. first is via 'action' attribute by adding a GET variable in to it, like action="mypage.php?form_id=1". and second way, which is imho more practical, is to name all inputs like an array. for example:

恕我直言有两种合理的方法可以在一个页面上识别两种形式。首先是通过'action'属性向其添加一个GET变量,如action =“mypage.php?form_id = 1”。第二种方式,即更实用,就是将所有输入命名为数组。例如:

<form>
  <input name="form1[first_name]" />
  <input name="form1[last_name]" />
</form>
<form>
  <input name="form2[first_name]" />
  <input name="form2[last_name]" />
</form>

then you have $_POST['form1']['first_name'] and so on..

然后你有$ _POST ['form1'] ['first_name']等等..