0

I have two separate forms with radio buttons. I need to make a button, clicking which submit two forms.

<form action="/second_vote" method="post" id="poll-view-voting" accept-charset="UTF-8">
    <div class="choices">
        <div class="form-item form-type-radios form-item-choice">
            <div id="edit-choice" class="form-radios">
                <div class="form-item form-type-radio form-item-choice">
                    <input type="radio" id="edit-choice-1" name="choice" value="1" class="form-radio">
                    <label class="option" for="edit-choice-1">1 </label>
                </div>
            </div>
        </div>
    </div>
    <input type="submit" id="edit-vote" name="op" value="Vote" class="form-submit">
</form>

<form action="/second_vote" method="post" id="poll-view-voting--2" accept-charset="UTF-8">
    <div class="choices">
        <div class="form-item form-type-radios form-item-choice">
            <div id="edit-choice--2" class="form-radios">
                <div class="form-item form-type-radio form-item-choice">
                    <input type="radio" id="edit-choice-3" name="choice" value="3" class="form-radio">
                    <label class="option" for="edit-choice-3">1 </label>
                </div>
            </div>
        </div>
    </div>
    <input type="submit" id="edit-vote--2" name="op" value="Vote" class="form-submit">
</form>

I try to do like this, but only sent the first form.

<script language="javascript" type="text/javascript">
    function submitDetailsForm() {
        $('#poll-view-voting').submit();
    }
</script>
  • 4
    You can't submit two forms in one page request. Either use AJAX or merge the forms to make one. – George Oct 09 '14 at 08:17
  • Why not use single form and indexed inputs? ``? Because you can't submit two forms, unless using Ajax. – Justinas Oct 09 '14 at 08:18
  • Using ajax is a better way – Amy Oct 09 '14 at 08:19
  • 1
    I sense a conceptual misunderstanding in the question. Can you give a valid use case (reason) why you need to submit two forms simultaneously and can't handle it server-side in a single submission? – pid Oct 09 '14 at 08:22

3 Answers3

0

I think that you can't send two form in one single request. You should use some AJAX request to send both of them in sequence, or, even better, use just one form (you're using the same action for both of your form..)

rvandoni
  • 3,297
  • 4
  • 32
  • 46
0

You can submit the first form using AJAX, otherwise the submission of one will prevent the other from being submitted.

Amy
  • 4,034
  • 1
  • 20
  • 34
-2

1

<form action="/second_vote" method="post" id="poll-view-voting--2" accept-charset="UTF-8">
    <div class="choices">
        <div class="form-item form-type-radios form-item-choice">
            <div id="edit-choice--2" class="form-radios">
                <div class="form-item form-type-radio form-item-choice">
                    <input type="radio" id="edit-choice-3" name="choice" value="3" class="form-radio">
                    <label class="option" for="edit-choice-3">1 </label>
                </div>
            </div>
        </div>
    </div>
</form>
<input type="button" value="Click Me!" onclick="submitDetailsForm()" />


<script language="javascript" type="text/javascript">
        function submitDetailsForm() {
            $('#poll-view-voting').submit();
            $('#poll-view-voting--2').submit();
        }
    </script>
Daniel Garcia Sanchez
  • 2,306
  • 5
  • 21
  • 35
  • This will not submit **both** forms. – George Oct 09 '14 at 08:20
  • 1
    More importantly, have *you*? Only 1 form will be submitted. – George Oct 09 '14 at 08:22
  • Look this ;) : http://stackoverflow.com/questions/7843355/submit-two-forms-with-one-button I have not tried, but seems to be submitted both forms – Daniel Garcia Sanchez Oct 09 '14 at 08:23
  • That case could have any manner of code listening for a submission and preventing it when it happens. In the case above, as is, you will only submit one form (make one page request) this way. – George Oct 09 '14 at 08:24
  • The comments in the accepted answer of the other question say the same thing: it might work, or it might not. It might (and will) fail in some browsers and situations. – JJJ Oct 09 '14 at 08:26
  • 1
    Best thing is play it safe and assume `submit()` is non-reentrant. – pid Oct 09 '14 at 08:28