23

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 = 1 and at the end of process $x = 0, so:

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

Probably, there is a better way.

2
  • 1
    So your question is not how to put two HTML form elements on the same page, but how to process the requests originating from submitting different forms with the same PHP pgae, isn't it?
    – ppeterka
    Dec 28, 2012 at 14:49
  • @ppeterka, yes, that's it.
    – Alegro
    Dec 28, 2012 at 14:50

6 Answers 6

54

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

5
  • A bit better description on why he'd need to do so would be good, but otherwise +1
    – ppeterka
    Dec 28, 2012 at 14:55
  • cristi_b, I have idea know. Thanks. I think - solved. BTW reasons is - consistent design.
    – Alegro
    Dec 28, 2012 at 14:57
  • you can implement my version or what Neal proposed, a form with 2 submit buttons (register and login)
    – cristi _b
    Dec 28, 2012 at 14:58
  • @cristi_b, interesting idea. Maybe I'll try.
    – Alegro
    Dec 28, 2012 at 15:02
  • Thanks, this is exactly what I needed.
    – Cyrill
    Jun 4, 2021 at 12:59
9

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;
}
3
  • yeah, same page, different submit buttons with different values on the same form ... a bit better than my solution
    – cristi _b
    Dec 28, 2012 at 14:57
  • i noticed here, when an answer appears on the screen, it comes already -1 votted !!?
    – Alegro
    Dec 28, 2012 at 15:00
  • you forgot to mention to add a hidden input
    – Marvin
    May 27, 2019 at 20:35
6

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
}
6

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>

1
  • 1
    very easy solution. Oct 19, 2020 at 9:11
3

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 }
1

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
}
4
  • And what if the register form does have username and password fields too? I think a registration form does usually have those...
    – ppeterka
    Dec 28, 2012 at 14:54
  • @ppeterka, yes, both forms have username and pass fields
    – Alegro
    Dec 28, 2012 at 14:55
  • I know it's not good to assume, but he doesn't provide much info so, I thought that he has one form with two divs(login, register) and when he clicks submit he must check the post data. It's not a good idea to operate this way, but let's hope that I my simple code was usefull for him.
    – gezimi005
    Dec 28, 2012 at 14:57
  • he have to put different names if he want to do the way he expressed. For example in register form changes names: new_username, new_password, confirmed_password.
    – gezimi005
    Dec 28, 2012 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.