PHP Javascript Single Form Call Submit in Two Forms

You saw?
submit the form inside another form

Passing form data to two locations using Javascript and PHP?

None of them answer the question.

Let me explain the scenario. I have a form that asks for a name, email address and phone number, and it should be sent in two places to two different providers.

1) Aweber 2) Company

One possibility is to create a piece of Javascript code that, when people click Submit, sends it to two form calls in two different browser windows (this would be my preferred method).

The second option is to send it first to Aweber (autorespnder), and then pass the variables to a custom PHP page. Extract the variables using $ _GET, and then submit them to the automatic submit form, which submits them to the second form, which is sent to the company.

For example - I can submit the Aweber form first, and then pass the results back to form.php:

$name = $_get['name'];
$email = $_get['email'];
$phone = $_get['phone'];

What does the rest of the form look like using curl or javascript?

I tried passing the variables to the company message form. It does not accept any GET commands - it only accepts a POST command, so CURL will probably not work (if there is no way to POST form via CURL?)

Does anyone have an elegant solution that doesn't look bad for end users?

What are your ideas? Thanks in advance!

+4
1

- , AJAX jQuery.

*

, , , .

HTML

<form id="test-form">
  <input type="text" id="name" name="name"/>
  <input type="submit" value="submit"/>
</form>

<p class="response1">
  Response 1 goes here
</p>
<p class="response2">
  Response 2 goes here
</p>

JQuery

$("#test-form").on("submit", function(e) {
  e.preventDefault(); //stops the form from actually submitting
  var root = 'https://jsonplaceholder.typicode.com';

  $.ajax({
    url: root + '/posts/1',
    method: 'POST',
    data: $("#test-form").serialize()
  }).then(function(data) {
    if(data) {
        $(".response1").text(data.title);
        $.ajax({
          url: root + '/posts/2',
          method: 'POST',
          data: $("#test-form").serialize()
        }).then(function(data) {
          if(data) {
            $(".response2").text(data.title);
          }
        });
    }
  });
})

, URL- , , API , post. . , .

Fiddle: https://jsfiddle.net/calder12/t0fos3kk/1/

+2

Source: https://habr.com/ru/post/1661247/


All Articles