Jump to content

How do I simulate submitting a form with PHP?


exodar

Recommended Posts

I have come across a situation that I have yet to program for and I am not sure how to resolve it.  I have a web form that has to do two things when the user presses the SUBMIT button.  First the HTML form has to POST all of the field variables to a PHP file so the file can insert a record into a database.  Second, that same PHP file has to POST some variables to PayPal to setup the correct payment screen for the user.  This all has to happen with the user pressing the SUBMIT button once on the original HTML form.

POST'ing the HTML form variables to the PHP file and inserting the record is easy...my five year old can do that part  ;)  What I cannot figure out is how to POST variables to PayPal and send the user to the PayPal payment screen from that same PHP file.  I can setup the original HTML form to SUBMIT and POST the variables to PayPal directly, but then I don't have any way to perform the database insert I need to prior to sending the user to PayPal.

I have looked at HTTP_POST_DATA and using FSOCKOPEN, but those seem to lack the ability to actually simulate the SUBMISSION of a HTML form where it directs the user to the corresponding page.  Of course, there is a good possibility that I have screwed something up in my use of those two functions.

Can anyone assist?  Thanks in advance...
Link to comment
Share on other sites

I am completely unfamiliar with using CURL.  It looks like it uses FOPEN and actually opens and closes a POST connection to a location.  Seems similar to HTTP_POST_DATA...which I was unable to get working.  Maybe both of these are just beyond my PHP knowhow (starting to look that way)...can someone assist me with an implementation of CURL that would do what I need?  I am assuming that I need to setup some sort of http header with all of the variables I want to post, but if I do this with CURL in my PHP file, will the user be redirected to PayPal like I want them to?  If it just POSTS the variables "behind-the-scenes" and doesn't take the user to PayPal, then it does me no good.

Sorry to seem like such a newb, but it is hard to avoid what you are :)

Thanks...
Link to comment
Share on other sites

All of us started as newbs :)

Try this code:

[code=php:0]$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.paypal.com/script');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'submit=Submit&variable=value');
$content = curl_exec($ch);[/code]


A lot of those are standard options.. it's just the last two that are specific to post forms.

A good way to get the data you need to put in POSTFIELDS is to use a firefox extension like LiveHTTPHeaders, UrlParams or Tamper Data.  That'll show you exactly what is being posted when you submit a form.  Then you can grab that and put it into your own script.

Keep in mind that all the variable values in postfields should be urlencoded.  Eg

[code=php:0]$variable_enc = urlencode($variable);
$postfields .= "&variable=$variable_enc";[/code]


BUT, if you never decoded your variables in the first place then you can pass them straight through.. just use trial and error :)
Link to comment
Share on other sites

Okay...I have experimented all morning with the code you provided.  Unfortunately I am left with the same results I had before.  When I press the Submit button on the form it calls my PHP which in turn attempts to POST via CURL, but I am just left with a blank screen.  I don't get any errors so I am assuming that CURL is working properly and phpinfo() reveals that CURL is enabled:

CURL support enabled
CURL Information libcurl/7.15.3 zlib/1.2.1.2 libidn/0.5.6

I put an echo $content; just after it to make sure that part of my PHP was actually being executed and it appears to be.

My assumption is that CURL is indeed POST'ing everything to the PayPal URL that I provide, but my browser just isn't being sent to that location.

Any ideas?
Link to comment
Share on other sites

well...not sure if onload will actually give me enough time to execute the insert statement I need though.  When the PHP file is called from the web form, it performs a database insert, then needs to submit a form to PayPal.  So to the user it will just take them to the PayPal payment screen after pressing submit even though the submit button they are pressing actually takes them to a PHP file first that does the database insert, THEN sends them to PayPal.  It is also important that the variables being sent to PayPal are being POST'ed.

Hope that makes sense...
Link to comment
Share on other sites

Sorry if that won't help or i understood wrong but do you control the page you want to redirect them to???

I mean if you want some values passed in another page of yours why don't you just create a hidden
form with the desired values and submit it from the previous page???

Hope i understood your problem

kathas
Link to comment
Share on other sites

I don't control the page I want to redirect them to.  It is PayPal's payment screen.  The problem I have is that when you submit a web form, it can only go to one location.  I can easily setup my web form to submit to PayPal and it works perfectly with all of the variables I send (payment, subscription interval, etc).  However, right before the user is sent to PayPal I need to insert a record into a database on my webserver.  So what I am trying to do is have the web form submit all the variables to a PHP file so that it can first insert my database record, then perform another submit of those variables to PayPal immediately afterwards.

So to the user when they press submit, they are just taken right to the PayPal payment screen with all of the correct amounts and everything there...but in reality, the SUBMIT button they pressed on the web form ACTUALLY took them to my PHP file, which inserted a database record first and THEN submitted all of the correct variables (via POST) to PayPal.

I just can't get my PHP file to send the users browser to PayPal...just get a blank screen after it inserts.

Thanks...
exodar
Link to comment
Share on other sites

Oh.. you want to redirect the user to the output of the paypal request?  That's not really possible using curl, since you need the user's browser to make the request.  You could do it with javascript..

Display a post form leading to paypal, with all the correct variables set (the form doesn't have to be visible to the user, since it will be submitted automatically).  Name the form "paypal_form", eg [code=php:0]<form name="paypal_form" method="post" action="http://www.paypal.com/...">[/code]

Then have some javascript that does

[code=php:0]document.paypal_form.submit();[/code]

I think it'll work if you put that javascript code down the bottom of the page.. the order of javascript code execution really confuses me!

On the page which does that submit, you can display a big "Please wait ...".  You could even make it in a pop-up, if you've ensured your users have turned off their pop-up blocker.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.