Invisibly Submitting a Form with PHP

Hello,

I’m building a script which connects to a payment gateway (via a form with hidden inputs) and on that page I’m also creating a database record of the order.

At the moment the record is created when the user visits the page, which will duplicate the data if the user returns to alter any details. This doesn’t have much of a negative effect, other than duplicate data in the order management area (which is not marked as a complete order).

What I would like to do is create the database after the user confirms the order and somehow create, populate and submit the form to the Payment Gateway in the background.

Any ideas?

Cheers,

Jon

It would be easier, but given the choice clients would like to know if they are losing customers at the point where they get directed elsewhere. It can also be a good indicator of a problem with the PSP, and this approach has saved me in the past.

Cheers,

Jon

have anybody tried Zend_Http_Client
http://framework.zend.com/manual/en/zend.http.client.html
You can find it very promising.

I think you’d probably be best to either edit your OP or provide an updated post with exactly what you’re looking to do, because right now I’m confused as to what it is you’re trying to accomplish (your posts seem to be contradictory).

I’m using Sagepay Form, but I want to capture details of people who might just close the browser when they get to the payment page (this could help pick up problems). I’ll automatically delete these details after a month.

I thought that there was a way of creating and sending a form using the header() function?

Cheers,

Jon

Wouldn’t it be a lot easier to simply set the form to update the database on return use?

Which Payment Gateway you are using? Paypal with IPN notifies when the transaction is successful and you can save the record after its confirmation.

Hi Jon,

  1. Build your form as normal
  2. Submit this form to a local script and save/log
  3. Use this post’d data to rebuild the form to hidden fields
  4. Optionally use JS to auto-submit the form (re-rendered page) to the payment gateway along with a unique identifier for the order.

I think you need to use either cURL to post in the background or depending upon your code process/flow.

 
$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

This is just an example copied from manual. There are lots of examples in user notes area in manual page from where you can get idea how to use.
http://www.php.net/manual/en/function.curl-setopt.php

Or you can also use the JavaScript form submission after certain action/event in the background something like this:

document.formname.submit();

Not sure what the confusion is, maybe I need to spell it out to some…

After the customer has seen the contents of the cart, added their name/address etc, they are direct to a script where they see all order details and then confirm the order.

As it stands the confirm button is actually a submit button for a form which contains only hidden inputs and is posted to the Payment Service Provider where they can take card details, approve/decline the order and send that information back to a specified page on the site where the database record is completed.

What I prefer to do is create part of the database record on the confirm page, so the client gets more information about where potential customers are aborting the checkout process, but if the decide to leave the confirm page to edit their details the database record is duplicated (not a huge problem).

I was trying to improve the script by having the confirm page’s submit button point to itself, create the database record after the customer has confirmed the order and then submit the details to the PSP (which would involve building and submitting a form) without further customer interaction.

I’m sure I’ve seen somewhere that I can do something like this with the header() function, and I was hoping that someone can point me in the right direction.

I like the JavaScript idea that was mentioned earlier but that would mean that I would have to take extra steps to make the shopping process work for the small minority who disable JavaScript.

I hope that clears it up for you.

Cheers,

Jon

You have a checkout script, when requested via GET it renders the cart and asks for supplementary details. It contains a submit button which POSTs to itself.

When this script is POST’d to, it saves/logs all the information and renders an HTML page containing hidden inputs containing all the values the Payment Gateway requires.

This HTML page (using JS) auto-submits to the gateway completing the sale (hopefully :slight_smile: ), for non-JS users you present a button stating ‘Make Payment’ or the like to submit the form.

The user merely sees…

  1. Checkout
  2. Add Information
  3. Confirm Order
  4. Make Payment (not seen if JS)

This allows you save cart contents should the transaction fail for whatever reason too.

“Hey Mr.Foo, you still haven’t paid for those Marshmallows…”