SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: Invisibly Submitting a Form with PHP

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,004
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to use either cURL to post in the background or depending upon your code process/flow.
    Code php:
    $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:
    Code javascript:
    document.formname.submit();

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Location
    Pakistan
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which Payment Gateway you are using? Paypal with IPN notifies when the transaction is successful and you can save the record after its confirmation.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't it be a lot easier to simply set the form to update the database on return use?

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SituationSoap View Post
    Wouldn't it be a lot easier to simply set the form to update the database on return use?
    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

  8. #8
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    248
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jonpugh114 View Post
    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
    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).

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Gloucestershire, UK
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SituationSoap View Post
    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).
    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

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,109
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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 ), 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..."
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have anybody tried Zend_Http_Client
    http://framework.zend.com/manual/en/...tp.client.html
    You can find it very promising.
    PHPycho - Magento Freelancer
    Free Modules: jQuery LightBoxes | Frontend Links Manager & more...
    Commercial Modules: Custom Login Redirect Pro | Store Restrction Pro & more...

    Follow me on Twitter @ magepsycho

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •