Maff
Maff

Reputation: 439

Sending form data to 2 places

I have inherited a system running on PHP4.4.9, however I'm needing to do something that requires 5.0 or higher, at current we cannot upgrade as it's part of a "package" (Hornbill Supportworks) that will break various feature and functionality. What I have done is added the php 5+ stuff to my personal webspace for now. An example of the form I have is:

<form action="./ChangeStatusSubmit.php" method="post" name="main">
<input type="text" size="15" maxlength="43" name="GappsMailTextS">
<button type="submit">
Submit status changes
</button>
</form>

In the form action I'm also wanting it to post to http://www.externalurl/test.php, can anyone help with this? I've not found anything that works online as it requires "curl" or ajax (I've never used/seen ajax so I would have no idea how to put that in to the file! any help appreciated.

Upvotes: 1

Views: 2327

Answers (3)

Vivian River
Vivian River

Reputation: 32390

As @aksu posted, AJAX is one tool that you can use to solve this problem.

However, if you want it to be processed as a form submission to two different servers, there is something else that might work for you.

The action attribute on the form tells where the form should be posted to. I think that you should be able to use Javascript to submit the form to one server, then modify the form so that the action attribute points to a different server, and then submit it again.

I'm not certain but you may also need to modify the target attribute to tell the browser not to open the new page in the same window as that could stop your Javascript.

Upvotes: 0

aksu
aksu

Reputation: 5235

You need to use AJAX for this. There isn't way to add two actions to the form. You can ajax your first action and then submit the declared form action in .done handler. Hopefully you have jquery, it's lot more difficult to use plain js for ajax.
Some links:
http://api.jquery.com/jquery.ajax/#example-0
How to make an AJAX call without jQuery?

So based on above examples the jquery solution could be this:

$('form[name=main]').submit(function() {
  return false;
  $.ajax({
    type: "POST",
    url: "http://www.externalurl/test.php",
    data: { GappsMailTextS: $('input[name=GappsMailTextS]').value() }
  })
  .done(function( msg ) {
    $('form[name=main]').submit();
  });
)};

Upvotes: 0

Brian A. Henning
Brian A. Henning

Reputation: 1511

Forms generally only post to one URL, because in general terms, a POST is no different than a GET, in that a browser asks a server for a URL and displays the result. (The only actual difference lies in where the input data is placed in the HTTP request, beyond the scope of this answer).

One approach would be to have the first server (the host for the form's action) send the data on to the second server and, if needed, incorporate the second server's response in the output sent back to the browser.

Upvotes: 1

Related Questions