简体   繁体   中英

PHP: Send form data to php script which then posts data to another form processor like zapier.com?

I use a 3rd party form processor which is zapier.com. The issue is, I need a way to redirect the user after submitting data to the 3rd party form processor. Zapier.com accepts post, get, and put submissions.

I was thinking of:

  1. Client submits form
  2. My php form-processor captures the data
  3. My php form-processor then Submits the data via POST or GET to the 3rd party form processor
  4. My php form-processor then redirects the user to a thank you page.

I might be over thinking this, but the only way I see of doing this is making a form that posts data that has been posted to it. Otherwise the form will just send my user to the 3rd party processor without redirecting them to whatever page I choose. The 3rd party form processor doesn't have a way of me using custom redirects.

Using javascript and Ajax it can be done like this (with jQuery):

$("#idOfTheForm").submit(function(e) {
    e.preventDefault();
    $.ajax({
        method : "post",
        url : this.action,
        data : $(this).serialize(),
        success : function() {
            window.location = "yourUrlOfThanks.html";
        },
        error : function() {
            alert("Something went bad");
        }
    });
});

So basically it is: sent a post request to the action url of this form, and once it throws an 200 code (found and everything went ok) then redirect to my page. If soemthing went wrong, then the server will throw an 40* status code and the script will go into error function.

You can use Guzzle to proxy the user request.

    $client = new Guzzle\Http\Client();

    $request = $client->post('/3rd.party.php')
        ->addPostField('user_field_1', $_POST['user_field_1'])
        ->addPostField('user_field_2', $_POST['user_field_2']);

    $response = $request->send();

    if ($response->isSuccessful()) {
        //show message
    }

The downside is that you can't be 100% sure that the form submission was indeed successful. In order to achieve that you could scrap the $request->getBody() and check if a known success message is present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM