5

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.

I tried my own way:

if ((isset($_POST["form-1"])) && (isset($_POST["form-2"])) {
    //SQL Insertion 
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Mac Taylor
  • 5,020
  • 14
  • 50
  • 73
  • 3
    No it's not possible you can submit only one form at a time. – Salil Apr 27 '10 at 09:20
  • yes , i know i can make a unit form , but i must use two different forms , and these forms are not respective – Mac Taylor Apr 27 '10 at 09:29
  • 1
    Mac, Can U specify the main reaso u r using two forms and need to submit them at one click.. if this gets clear solution could me more comprehensive.... – OM The Eternity Apr 27 '10 at 09:30

6 Answers6

4

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?

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
3

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.

o0'.
  • 11,739
  • 19
  • 60
  • 87
1

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 :

<form action="doFoo.php">
    User <input type="text" name="username" />
    Pass <input type="password name="password" />

    <!-- Submit one -->
    <input type="submit" name="submit" value="Create user" />

    <!-- some more of your fields or whatever -->
    <input type="text" name="blah" value="bleh" />

    <!-- Submit two -->
    <input type="submit" name="submit" value="Login user" />
</form>

doFoo.php :

<?php
if( $_POST["submit"] == "Login user" ) {
    //do login foo    
}
if( $_POST["submit"] == "Create user" ) {
    //do signup foo 
}
?>
Kalyan02
  • 1,416
  • 11
  • 16
0

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

rojoca
  • 11,040
  • 4
  • 45
  • 46
0

As far as i know only one form can be submitted at a time. You could try wrapping them in one form.

Rob
  • 6,731
  • 12
  • 52
  • 90
0

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.

naivists
  • 32,681
  • 5
  • 61
  • 85