Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place two forms on the same page?

Tags:

html

forms

php

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.

like image 543
Alegro Avatar asked Dec 28 '12 14:12

Alegro


People also ask

Can you have multiple forms on a page?

You can handle multiple separate forms on a single page by including a hidden field identifying the submitted form. You can use formsets to create multiple different instances of the same form on a single page.

Can you have 2 forms on an HTML page?

HTML standard dictates no two elements can have the same Ids. You can use Javascript to change the Id/Name dynamically before you hook the connector to the target form.


2 Answers

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

like image 169
cristi _b Avatar answered Oct 14 '22 20:10

cristi _b


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;
}
like image 44
Naftali Avatar answered Oct 14 '22 20:10

Naftali