How to place two forms on the same page?

后端 未结 6 956
借酒劲吻你
借酒劲吻你 2020-11-28 10:18

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

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

so, I need something l

相关标签:
6条回答
  • 2020-11-28 10:31

    Well you can have each form go to to a different page. (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;
    }
    
    0 讨论(0)
  • 2020-11-28 10:35

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

    Form one button - btn1 Form two button -btn2

    PHP Code:

    if($_POST['btn1']){
        //Login
    }elseif($_POST['btn2']){
        //Register
    }
    
    0 讨论(0)
  • 2020-11-28 10:35

    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>

    0 讨论(0)
  • 2020-11-28 10:41

    You could make two forms with 2 different actions

    <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.

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

    0 讨论(0)
  • 2020-11-28 10:41

    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
    }
    
    0 讨论(0)
  • 2020-11-28 10:49

    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:

    if (isset($_POST['btnPostMe1'])) { //your code 1 }
    if (isset($_POST['btnPostMe2'])) { //your code 2 }
    
    0 讨论(0)
提交回复
热议问题