简体   繁体   中英

Two login forms to be processed by one php script?

Have two login forms on one page and two database tables: admin( user , pass ) and customer( fname , laname , email , user , pass ). One for customer, and one for admin. Want to process both forms inside login.php

How to determine which form was Submit button clicked on and access user and pass fields from both forms inside login.php when trying to store $_POST username and password inside respective variables?

Something like this?(goes into login.php):

if(//submit button in customer form was clicked){
$user=$_POST['customer_login']['user'];
$pass=$_POST['customer_login']['pass'];
}else{
//admin submit button was clicked
$user=$_POST['admin_login']['user'];
$pass=$_POST['admin_login']['pass'];
}

Form(HTML):

<form name="customer_login" method="post" action="login.php">
    <h3>Customer:</h3>
    Username: <input type="text" id="user" name="user"><br>
    Password: <input type="password" id="pass" name="pass"><br>
    <input type="submit" name="Submit" value="Sign in">
</form>  

<form name="admin_login" method="post" action="login.php">
    <h3>Admin:</h3>
    Username: <input type="text" id="user" name="user"><br>
    Password: <input type="password" id="pass" name="pass"><br>
    <input type="submit" name="Submit" value="Sign in">
</form> 

Give the submit buttons unique names and/or identifiers and check which is set. From memory, that should work, but otherwise you could use a hidden field per form to indicate a predefined value you know, knowing this value you can process the appropriate data.

Change the name attrbute in one of your forms and then on php side check which $_POST values you get. For example:

<input type="submit" name="Submit2" value="Sign in">

if($_POST['Submit2']){
  //form 2 has been sent
}

Create a post name with customer login only for the customer form.

if(isset($_POST['customer_login'])){
    $user=$_POST['customer_login']['user'];
    $pass=$_POST['customer_login']['pass'];
}else{
    //admin submit button was clicked
    $user=$_POST['admin_login']['user'];
    $pass=$_POST['admin_login']['pass'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM