How to use two forms and submit once?

前端 未结 6 1146
广开言路
广开言路 2020-12-11 07:20

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

相关标签:
6条回答
  • 2020-12-11 07:21

    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 
    }
    ?>
    
    0 讨论(0)
  • 2020-12-11 07:21

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

    0 讨论(0)
  • 2020-12-11 07:22

    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?

    0 讨论(0)
  • 2020-12-11 07:33

    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.

    0 讨论(0)
  • 2020-12-11 07:36

    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.

    0 讨论(0)
  • 2020-12-11 07:46

    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

    0 讨论(0)
提交回复
热议问题