How to capture data to my database, then redirect user directly to the PayPal screen?

Posted on 2011-04-30
Last Modified: 2012-05-11
This is the scenario I want:
1. User clicks "Buy Now"
2. User fills out a form with name, phone, etc, then clicks "Pay Now"
3. The form is submitted to my php script where it;
   a) sends some data to my database
   b) redirects user to the Pay Pal Pay Screen
4. User logs in to Pay Pal, or Enters Credit Card info to complete purchase.

Right now, the only way I can achieve this is by having a "middle page" that has a form with the necessary fields for paypal (IE: bussines email, notify url, amount,etc), and a Button. So the user has to click this button to submit the values to Pay Pal so that it can create the Payment screen.

I want to eliminate that middle page and somehow do a redirect to Pay Pal with these POST variables.

I know I can "fake it" by using a javascript to submit the form automatically, so the user wouldn't have to push the button, but isn't there a better way to do this in php?

Thanks for any help :)

Question by:tonyhhisc
    LVL 18

    Accepted Solution

    Try this example, it uses jquery and AJAX.. I have been in your position before and this is what I used to solve it:
    <script src="//" type="text/javascript"></script>
    	function save_data(){
    		$.post("insert_into_db_page.php", { 
    			amount: ""+$("#amount").val()+"", 
    			shipping: ""+$("#shipping").val()+"" 
    <form action="" method="post" id="paypal_form">
        <!--You will have all your paypal information here but make sure you have an id on each like the amount example -->
        <input type="hidden" name="amount" id="amount" value="50.00" />
        <input type="hidden" name="shipping" id="shipping" value="10.00" />
        <input type="submit" name="submit" style="display:none;" id="real_submit"/>
        <input type="button" name="submit" value="Go To PayPal" onclick="save_data();" />

    Open in new window


    Author Comment

    Ahh, thats a good idea, basically doing 2 Post's at once. I like this, thanks :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now