HTML - 两个带有“输入”键的表单,以了解我正在输入的表单

HTML - Two forms with 'enter' keypress to know which form I am entering

本文关键字:表单 输入 了解 两个 HTML      更新时间:2023-09-26

我有两个表单,我想让输入一个表单文本字段并按回车键变得容易,页面知道正在填写什么表单。

表单 1(例如:搜索):

<form action="" method="post" name="form1">
<input type="text" name="txt1" />
<input type="submit" value="Enter 1" />
</form>

表格 2(例如:登录):

<form action="" method="post" name="form2">
<input type="text" name="txt2" />
<input type="submit" value="Enter 2" />
</form>

两者都通过PHP脚本进行验证并转到其正确的站点。搜索将添加到包含在每个页面 (MVC) 标题中的页面,并且登录名位于其自己的页面上,但两者都以两种形式组合在一个页面中。 在登录页面上登录时,我输入用户名和密码并按回车键,但它默认为搜索提交按钮,并想知道它在登录提交按钮上输入。

感谢您的帮助...

如果你给你的提交按钮一个名字,你将能够在PHP中检测到它们。

<input type="submit" name="submit" value="Enter 2" />

及以后

if ($_POST['submit'] == 'Enter 2') // ...

由于您知道在每个表单中要查找的字段名称,因此您可以将其键调:

<?php
if (isset($_POST['txt1']) {
    // do one thing
} else {
    // do the other
}
<form action="" method="post" name="form1">
<input type="hidden" name="form" value="1" />
<input type="text" name="txt1" />
<input type="submit" value="Enter 1" />
</form>
<form action="" method="post" name="form2">
<input type="hidden" name="form" value="2" />
<input type="text" name="txt2" />
<input type="submit" value="Enter 2" />
</form>

intval() 将验证发布的值。

if (intval($_POST['form']) == 1){}
elseif (intval($_POST['form']) == 2){}

摘自本文: http://www.javascript-coder.com/html-form/html-form-submit.phtml#multiple

多个提交按钮

一个表单中可以有多个提交按钮。但是,如何从服务器端识别按下了哪个按钮来提交表单?

一种方法是为提交按钮使用不同的名称。

<input type="submit" name="Insert" value="Insert">
<input type="submit" name="Update" value="Update">

在服务器端脚本中,您可以进行如下检查:

if(!empty($_REQUEST['Update']))
{
  //Do update here..
}
else
if(!empty($_REQUEST['Insert']))
{
  //Do insert Here
}

第二种方法是为具有相同名称的提交按钮设置不同的值。

<input type="submit" name="Operation" value="Insert">
<input type="submit" name="Operation" value="Update">

服务器端代码是这样的(PHP 代码):

if($_REQUEST['Operation'] == 'Update')
{
  //Do update here..
}
else
if($_REQUEST['Operation'] == "Insert")
{
  //Do insert here
}