Skip to content Skip to sidebar Skip to footer

How To Use Two Forms And Submit Once?

Is it possible to have two forms with two submit buttons such that when I click on the button it saves the input fields in both forms? I'm concerning to solve this in PHP / MySQL.

Solution 1:

Nope, you can only submit one form at a time.

If you have to use two forms, the only way to do this would be to clone the second form's fields into the first one using jQuery. Won't work when JS is turned off, though.

See Copying from form to form in jQuery

Why do you need two forms?

Solution 2:

If you have a problem like this, the design is flawed.

You can submit only one form at a time for a reason.

Change the design to use only one form; doing workarounds to submit two anyway is a horrible practice that would be better to avoid.

Solution 3:

One way of achieving similar result would be to club the two forms into a single one and have 2 submit buttons with different values and same name="submit" field.

toFoo.html :

<formaction="doFoo.php">
    User <inputtype="text"name="username" />
    Pass <inputtype="password name="password" /><!-- Submit one --><inputtype="submit"name="submit"value="Create user" /><!-- some more of your fields or whatever --><inputtype="text"name="blah"value="bleh" /><!-- Submit two --><inputtype="submit"name="submit"value="Login user" /></form>

doFoo.php :

<?phpif( $_POST["submit"] == "Login user" ) {
    //do login foo    
}
if( $_POST["submit"] == "Create user" ) {
    //do signup foo 
}
?>

Solution 4:

You could submit both forms at the same time via Ajax but your php script would only receive one form at a time. Better to just convert your 2 forms into one big form if you need all inputs going to one script

Solution 5:

A submit button submits only fields of the form it lives in. If you need content of both forms, you'll have to copy the fields from other form to some hidden field in the form where submit button was clicked. This can quite easily be done in JavaScript.

Post a Comment for "How To Use Two Forms And Submit Once?"