PHP: Save data in database before redirecting to PayPal

If you want to learn how to save data first to your precious database before redirecting to PayPal then stick around.

Sometime you want to save the information entered in your form before redirecting to PayPal payment. The problem with web forms is that when you submit the it, it has only one way post submission before redirecting to a page. Unfortunately you can’t submit a form with two actions being called (to redirect to PayPal and to save to the database), that’s where Ajax and JavaScript comes into action.

You can do this in many way but it’s more convenient to use Ajax for this.  With Ajax, Web applications can send data to and retrieve from a server asynchronously (in the background) without interfering with the display and behavior of the existing page. So eventually before the form being sent to PayPal, the Ajax script will be executed first (your save to database script) and will allow you to execute certain processes afterwards (like redirecting to a page).

So if you want to save the information from your form to your database you can do this trick.
Your form will be something like this together with the PayPal form fields you specified.

Save HTML form POST to database before redirecting to PayPal

HTML form

Above is the HTML form generated by PayPal. In addition three input fields are added to capture some useful information from the users. These information will be first save to the database before redirecting to PayPal via form submit.

Put it in the header of the HTML.

The Ajax script recognizes a form, identified by an ID #senrollnow that is being triggered by submission. The scripts determines if a ‘pending‘ class is in the form, if found, it stops the submission for a while and trigger a process which involves calling a certain page named insert.php. Insert.php is the script for inserting your form POST data to the database.

By adding the ‘pendingclass, the scripts continues to execute, afterwards the form submission redirecting to PayPal continues also.


The idea is simple but very useful make sure you are calling the right ID for your form and the insert script is certainly running. Now you will be able to save data to your database before redirecting to PayPal.

So that’s it. Just feel free to leave comments if you have a certain problem with the code.


Sharing is caring!

  • You can get paid in this way for doing something you probably would have already done; suggesting ssomething
    for someone to try. Brushing up on your management and marketing skills can be very helpful.
    There is penty of great free information out there on what are the basics of a search-engine friendly website.

  • I must thank you for the efforts you have put in writing this site.
    I really hope to view the same high-grade blog posts by
    you later on as well. In fact, your creative writing abilities has inspired
    me to get my very own blog now 😉

  • Hello everybody, here every one is sharing these kinds of experience,
    thus it’s nice to read this weblog, and I used to pay
    a quick visit this website every day.

  • A casuística de nosso estudo foi composta de acompanhantes de infantes internadas na enfermaria pediátrica do
    Sanatório Municipal de Urgência Henrique Sérgio Gregori, um sanatório público e de médio porte, do câmara municipal de Resende, Estado do Rio de Janeiro, no período de adolescência
    a julho de 2006.

  • Thanks for your personal marvelous posting!
    I genuinely enjoyed reading it, you’re a great author.I will
    remember to bookmark your blog and will often come back in the
    foreseeable future. I want to encourage you to continue your great work,
    have a nice weekend!

  • I do agree with all of the concepts you have presented oon your post.
    They’re very convincing andd will definitely work.
    Nonetheless, the posts are too brief for beginners. May you please lengthen them a bit from next time?
    Thanks for the post.

  • I read this piece of writing completely regarding the resemblance of
    latest and previous technologies, it’s amazing article.

  • I read this article completely regarding the difference of hottest and previous technologies, it’s amazing article.

  • Μany on-line floԝer supply providers prominentⅼy characteristic
    еco-pleasant plants and flowers.

  • Supriya Naik

    Hello, you have given a very good writeup on how to save database before moving on to the PayPal page. This helps in saving the user information.
    I tried executing the scripts mentioned above, but noticed that the data is not being saved in the database. Is there anything missing in the above scripts?

    • GeoD

      Hi it pretty much that’s it. You can test where your code is lacking, first check if the insert.php can save values in your database. you can do it manually. 2nd, if the php can save data, you can check the ajax if you are referencing the right directory to the php and if the values your are passing are available.

      • Supriya Naik

        Hi IKong, Thank you for your response.
        My insert.php works fine manually. But data not been saved from the form. Below is the script and form I am using. Can you please assist where I am going wrong?

        $(function () {
        $(‘#senrollnow’).on(‘submit’, function (e) {
        var form = $(this);
        if(!form.hasClass(‘pending’)) {
        type: ‘post’,
        url: ‘insert.php’,
        data: $(‘#senrollnow’).serialize(),
        success: function () {
        alert(‘form was submitted’+data);

        $(‘#freeform’).on(‘submit’, function (e) {
        //e.preventDefault(); //prevents the form to submit
        alert(“Your request has been sent! Please check your email within 12 hours for my feedback. Thank you.”);

        Name *
        Company *
        Email *