php 如何在同一页面上放置两个表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14071250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:33:58  来源:igfitidea点击:

How to place two forms on the same page?

phphtmlforms

提问by Alegro

I want to place both register and login form on the same page.
They both starts with:

我想将注册和登录表单放在同一页面上。
他们都开始于:

if (!empty($_POST)) ... 

so, I need something like:

所以,我需要类似的东西:

if (!empty($_POST_01))...  // regForm
  and 
if (!empty($_POST_02))...  //loginForm

Also how to prevent executing first form if the second is busy, and vice versa (user clicks on both)
My idea is to create a simple variable on starting process, for example $x = 1and at the end of process $x = 0, so:

还有如何防止在第二个表单繁忙时执行第一个表单,反之亦然(用户同时点击两者)
我的想法是在启动过程中创建一个简单的变量,例如$x = 1,在过程结束时$x = 0,所以:

if ((!empty($_POST_01)) And $x = 0)...

Probably, there is a better way.

也许,有更好的方法。

回答by cristi _b

You could make two forms with 2 different actions

你可以用 2 个不同的动作制作两个表格

<form action="login.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="submit" value="Login">
</form>

<br />

<form action="register.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="submit" value="Register">
</form>

Or do this

或者这样做

<form action="doStuff.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="hidden" name="action" value="login">
    <input type="submit" value="Login">
</form>

<br />

<form action="doStuff.php" method="post">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="hidden" name="action" value="register">
    <input type="submit" value="Register">
</form>

Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.

然后您的 PHP 文件将用作 switch($_POST['action']) ...此外,它们不能同时单击两个链接或同时发出请求,每次提交都是一个单独的请求。

Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure

然后您的 PHP 将继续切换逻辑或使用不同的 php 文件执行登录程序然后执行注册程序

回答by Naftali aka Neal

Well you can have each form go to to a differentpage. (which is preferable)

那么您可以让每个表单转到不同的页面。(这是可取的)

Or have a different value for the a certain input and base posts on that:

或者对某个输入和基础帖子有不同的价值:

switch($_POST['submit']) {
    case 'login': 
    //...
    break;
    case 'register':
    //...
    break;
}

回答by mark kasina

Give the submit buttons for both forms different names and use PHP to check which button has submitted data.

给两个表单的提交按钮指定不同的名称,并使用 PHP 来检查哪个按钮提交了数据。

Form one button - btn1 Form two button -btn2

一键成型-btn1 成型二按键-btn2

PHP Code:

PHP代码:

if($_POST['btn1']){
    //Login
}elseif($_POST['btn2']){
    //Register
}

回答by gezimi005

Hope this will help you. Assumed that login form has: username and password inputs.

希望这会帮助你。假设登录表单有:用户名和密码输入。

if(isset($_POST['username']) && trim($_POST['username']) != "" && isset($_POST['password']) && trim($_POST['password']) != ""){
//login
} else {
//register
}

回答by Rayobeats

You can use this easiest method.

您可以使用这种最简单的方法。

<form action="validator.php" method="post" id="form1">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="submit" value="submit" form="form1">
</form>

<br />

<form action="validator.php" method="post" id="form2">
    <input type="text" name="user">
    <input type="password" name="password">
    <input type="submit" value="submit" form="form2">
</form>

回答by Adem Büyük

Here are two form with two submit button:

这是两个带有两个提交按钮的表单:

<form method="post" action="page.php">
  <input type="submit" name="btnPostMe1" value="Confirm"/>
</form>

<form method="post" action="page.php">
  <input type="submit" name="btnPostMe2" value="Confirm"/>
</form>

And here is your PHP code:

这是您的 PHP 代码:

if (isset($_POST['btnPostMe1'])) { //your code 1 }
if (isset($_POST['btnPostMe2'])) { //your code 2 }