-3

How can I have multiple forms SUBMITTED/POSTED to PHP with one submit button? Is there an easy html way to achieve this? I need to use traditional $_POST variable on the PHP side that's why.

Example

<form class="form-horizontal" method="post">

    // SOme fields

</form>

<form class="form-horizontal" method="post">

    // SOme more fields

</form>


<form class="form-horizontal" method="post">

    // some fields here

    <button type="submit" class="btn btn-success">Stuff</button> // This button posts all three forms!
</form>
user1952811
  • 2,398
  • 6
  • 29
  • 49
  • A downvote in less than 10 seconds? how is that even possible? – user1952811 Jul 10 '14 at 14:31
  • 3
    No, a submit is only going to work for the form in which it is nested. You're going to need ajax. http://stackoverflow.com/questions/8563299/submit-multiple-forms-with-one-submit-button – VikingBlooded Jul 10 '14 at 14:34
  • Why do you need three forms on a page? This seems like a bad idea. Why can't you just post all the fields in one form? – Tim Jul 10 '14 at 14:44
  • Issue is my other fields are in a modal. Modals need to stay on top of the page and so I can't just ad them in to the form. – user1952811 Jul 10 '14 at 14:46
  • Why not? Group them into a fieldset or a div, and use that for your modal … – CBroe Jul 10 '14 at 14:48
  • @CBroe if I put my modals inside form, my page starts looking really funky. – user1952811 Jul 10 '14 at 14:56
  • To literally submit multiple forms, you need AJAX. Period. But I almost guarantee that if you are wanting to submit multiple 'sets' of data at once, you can do it otherwise. – Andrew Barber Jul 10 '14 at 15:11
  • Multiple sets of data that are not contained in form markup. – user1952811 Jul 10 '14 at 16:38

1 Answers1

1

You can do something like:

<form class = "form-horizontal" method = "post>
    <select id = "select" name = "selection">
        <option name = "option_one" value = "html">html</option>
        …
    </select>

    <input type = "checkbox" name = "selected[]" value = "first_value">first</input>
    <input type = "checkbox" name = "selected[]" value = "second_value">second</input>

    <input type = "checkbox" name = "another_selected[]" value = "new_first">another first</input>
    <input type = "checkbox" name = "another_selected[]" value = "new_second">another second</input>
    <input id = "submit" name = "submit" type = "submit"></input>
</form>

Then in PHP:

$selected = _POST["select"];
$first_checkbox_group = _POST["selected"];
$second_checkbox_group = _POST["another_selected"];

Just make sure you give you different sections different names.

timtour97
  • 222
  • 1
  • 3
  • 10