0

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.

Damainman
  • 515
  • 9
  • 21
  • with this scheme, you may not have a way to know if the information was successfully submitted to the 3rd party form processor. Also, if you have some sample code/test page for this then that would be helpful. – Maximus2012 Sep 09 '13 at 21:23
  • Right now its just a regular html form with the URL of the 3rd party form processor in the "action" field of the form. But I need a way to also redirect the user to a page I determine, after they submit the form instead of them going to the 3rd party processor. – Damainman Sep 09 '13 at 21:25
  • What about curl to post the data to the third party processor? – Pep Lainez Sep 09 '13 at 21:39
  • 1
    This sounds like you can just POST/GET the form data directly through an ajax call to the third party processor. I don't know what this third party processor is, but it should be able to respond to your AJAX call. – Serguei Fedorov Sep 09 '13 at 21:44
  • Pep, I can only use post, get, or put for the 3rd party processor. @SergueiFedorov , I attempted to do it via ajax utilizing the answer from AbrahamSustaita but I am encountering error triggers even though the form successfully submits. I posted that issue here: http://stackoverflow.com/questions/18743487/successful-ajax-post-displays-error-message – Damainman Sep 12 '13 at 14:20
  • Right. This is something you have to figure out with the API rather than complicate things by trying to side step it. Good chances are you will get the same issues trying to do it some elaborate way. If they have an API documentation take a look at that; it could be expecting something along with the form data. If the AJAX request is the recommended way of sending form data, then that is how you should do it. You can probably always contact them or post on their forums with this issue. If you have access to their API, you can probably look through it to see what you need to change. – Serguei Fedorov Sep 12 '13 at 14:56
  • I am using their basic webhook which accepts get, post, and put entries to a unique url. Everything is working perfect, and it is processing the posts from my form. Their API appears to be if I want a full fledge web service to integrate with their web service. But I only want to submit a form and utilize their prebuilt hooks, which works for me minus the error code invoked via ajax even with successful submissions. Because of the error invoked via ajax, I am unable to redirect the user to a thankyou page after the form submission. Figured there might be an easier way to make a thank you page. – Damainman Sep 12 '13 at 15:15

2 Answers2

0

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.

Cito
  • 1,659
  • 3
  • 22
  • 49
  • I keep getting the error popup message instead of redirecting to the success URL. I can however confirm that the data is being properly posted and I am receiving a 200 code according to firebug via the console. Any ideas? – Damainman Sep 11 '13 at 13:49
  • @Damainman can you give some html of what the third party is writing tho the page? It seems to me, if the request was done via ajax before addint the jQuery script, it is capturing the event and stoping it from bubbling to the rest of js. Does it add js? Is it done with ajax without the code I gave you? url of example? Name of third party script? – Cito Sep 11 '13 at 18:03
  • I created a new question for the issue I was experiencing with your ajax method here: http://stackoverflow.com/questions/18743487/successful-ajax-post-displays-error-message?noredirect=1#comment27627096_18743487 – Damainman Sep 12 '13 at 14:13
  • Looks like it be related to crossdomain ajax issues since I am trying to post to a 3rd party processor. – Damainman Sep 12 '13 at 14:32
0

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.

xmarcos
  • 3,298
  • 2
  • 20
  • 27
  • Oh, and make sure you sanitize the data you get from $_POST! – xmarcos Sep 09 '13 at 22:12
  • Thank you for the suggestion. I would need to make sure that the form submission was indeed successful. However the page would not really submit a known success message. Would checking for the http 200 code be sufficient? However I don't have access to the server configurations to install the necessary components for this. – Damainman Sep 12 '13 at 14:17
  • As I said, you could scrap the the page returned to make sure it was successful instead of just checking the status code returned. You can use https://github.com/fabpot/Goutte for that. – xmarcos Sep 12 '13 at 18:15
  • And you don't need composer to use those libraries, just download them and use them like any other php library. – xmarcos Sep 12 '13 at 18:17