0

I have a simple form that submits some data to an SMS server to send an SMS. I need to create two seperate messages from one button. message1 needs to go immediately and message2 needs a delay, which the gateway supports.

I Need two different "message" fields, one with the delay and one without. I dont think the server allows two commands from one submission. Is there a way of getting the forms to submit sequentially from one button ?

The fields that are common to both messages are :

<form action="https://api-mapper.clicksend.com/http/v2/send.php" method="post">

<input name="key" type="hidden" value="xxxxxxxxx" />

To: <input name="to" type="text" />

<input name="username" type="hidden" value="xxxxxx" />

<button type="submit" >SUBMIT</button>

the fields that cause me issues are:

<input name="message" value="message1" />
<input name="message" value="message2" name="schedule" value="time" />
  • Is the form being posted to a 3rd party, or to your own back-end system which integrates with that 3rd party? If the former, are AJAX requests supported? – David Oct 30 '19 at 15:43
  • why not use ajax to send a post request. perhaps you could use a javascript `submit` and assign a `class` or `id` in the form? – Alvin Quezon Oct 30 '19 at 15:47
  • Does this answer your question? [Submit two forms with one button](https://stackoverflow.com/questions/7843355/submit-two-forms-with-one-button) – Diogo Cruz Oct 30 '19 at 15:47
  • the server is a third party company and i have no control over them. I have never used AJAX - if someone is kind enough to provide a brief tutorial or some code i would be over the moon. Im only an amateur at all this! – billyqureshi Oct 30 '19 at 15:52
  • Diogo - potentially yes. are you able to help me assign form IDs or form names to my forms? – billyqureshi Oct 30 '19 at 15:56
  • Hi @billyqureshi, you can simply assign an id like this `
    ` then on your script you can assign the id like this `document.getElementById("name_your_id_here_without_space").submit();` the id should correspond to the name in your formid and make sure it the id is unique in every form.
    – Alvin Quezon Oct 30 '19 at 16:09

1 Answers1

0

try this.

<form id="form1" action="action.php", method="post">
   <input name="key" type="hidden" value="xxxxxx"/>
   <span>To:</span> <input name="to" type="text" />
   <input name="username" type="hidden" value="xxxxxx" />
</form>

<form id="form2" action="action2.php", method="post">
   <input name="message1" value="message1" />
   <input name="message2" value="message2" name="schedule" value="time" />
</form>

<button type="submit" onclick="submitForms">SUBMIT</button>

<!-- add this tag after your body tag -->
<script>
   var submitForms = function() {
      document.getElementById("form1").submit();
      document.getElementById("form2").submit();
   }
</script>
Alvin Quezon
  • 1,089
  • 3
  • 11
  • 29
  • 1
    The browser will start to submit form1 and navigate to the response, but since you instantly submit another form, the first request will be cancelled and it will submit the second form **instead** (not as well). – Quentin Oct 30 '19 at 16:38
  • i cant get this to submit the second form – billyqureshi Oct 30 '19 at 16:40
  • @Quentin can you review Alvin's suggestion? – billyqureshi Oct 30 '19 at 16:51
  • @Quentin yes right, my bad i never thought of that. – Alvin Quezon Oct 31 '19 at 03:08
  • 1
    @billyqureshi, it is because the button is inside the `form1` and when trying to send it, the only thing it submit is the `form1` and `form2` will not submit because submit button only performs inside the `form1` that is the reason why it will skipped the `form2` submission. Enable to fix this, I have to move the submit button from the `form1` then put it outside of the forms. In this way the `submitForms` function will be the one to handle all the post request for both forms. – Alvin Quezon Oct 31 '19 at 03:21