0

I am trying to send a form to 2 different processors. One is a 3rd party cloud database that I do not control nor have the code for. The other my simple contact form processor. I was trying something like:

<form action="mail.php" method="POST">
<input name="name" type="text" />
<input type='submit' name='submit' onclick="this.form.action="//3rdpartcloud.com";" />

From what I understand this wont work because it leaves the page on the 1st action and cant do the 2nd action.

I have seen Ajax suggestions, but no clear example. But as I will be sending the same variables to both files, I was thinking it would be easier to POST all variables to my php form, and then from my php to automatically POST them to the 3rd party server. I do not know how they process the data so I want to send it as form values not as variables.

Is there a simpler way to achieve this? What is the correct syntax? I'm guessing something like this in the php:

<form action="site.com" method="POST">
<input name="name" value=$name>

But even if that works - how do I auto send it to an action url?

Jon
  • 6,227
  • 6
  • 35
  • 58
  • Ajax is the way to go. Clear example: http://stackoverflow.com/questions/1960240/jquery-ajax-submit-form – Alex Blex Jan 18 '16 at 12:57
  • Again no clear code on how to send to 2 actions, and would need the browser using javascript. – Jon Jan 18 '16 at 13:01
  • Consecutively. First, then second. Yes, browser will need to use javascript. – Alex Blex Jan 18 '16 at 13:06
  • The link also uses Jquery, I would prefer not to use it. If it is done in php no client side frameworks needed – Jon Jan 18 '16 at 13:12
  • Then use curl http://php.net/manual/en/book.curl.php to submit form data from your server. – Alex Blex Jan 18 '16 at 13:30

1 Answers1

1

First sumit your form using ajax to your own site - then submit the form to the other site as usual.

Your Ajax - something look something like this:

<form id="form" action="theothersite.php" method="POST">
    <input name="name" type="text" />
    <input type='submit' id="submitbutton" />
</form>

<script type="text/javascript" >

$('#submitbutton').click(function(e){

   e.preventDefault();  //stops form submission

   $.ajax({
       type: "POST",
       url : '/yoursiteurl.php',
       data: $('#form').serialize(),
       success: function(data){
           $('#form').submit(); //now submit the form
       }
    });

})

</script>

In your PHP do a:

print_r($_POST);

To see the submitted values

Jason Butler
  • 807
  • 7
  • 12
  • How does this pass the data to the php file? print_r($_POST['formdata']); shows nothing. (also needs to add a '). The form is already to process the data as is. And the question was how to do it through php? – Jon Jan 18 '16 at 16:39
  • I've updated my answer - the post vars should match whats posted by the form - I've just ditched the 'formdata' var and serialized the form directly into the ajax data. You should be able to preview all posted data with print_r($_POST) – Jason Butler Jan 18 '16 at 17:35