Home > front end >  How to recognize which form to handle in PHP
How to recognize which form to handle in PHP

Time:04-22

I have a PHP file called processLogin.php (mainly use for login and register) and inside it, contains 2 HTML <form> elements. Each form will have a submit button. And i am using $_SERVER['PHP_SELF'] to handle the form.

So is there any way to recognize which form submit to handle, like login form ? or register form. Because both of these <form> elements have the action attribute is $_SERVER['PHP_SELF']

My code: <form id="registerUserForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

and <form id="loginUserForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

CodePudding user response:

Normally, you can handle form by checking which form was clicked.

Like this.

<form id="loginUserForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

<input type="submit" name="login" value="Login" />
</form>

<form id="registerUserForm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

<input type="submit" name="register" value="Login" />
</form>

And then write the php code to handle the form like below

<?php
if(isset($_POST['register')){

// Write code to handle registration here

}

if(isset($_POST['login')){

// Write code to handle login here

}

?>
  • Related