1

I have a PHP shopping basket on my site. The shopping basket contains a form with the list and values of the items.

It looks something like this:

<form action="https://www.sandbox.paypal.com/us/cgi-bin/webscr" method="post">
<input....etc etc etc>
    <input....etc etc etc>
<input type="submit" name="add-to-basket" value="Buy Now">
</form>

When the user clicks on "Buy Now", I want to run some final checks [in a function] before the form is submitted to Paypal. If the final checks are passed then the user will be taken to Paypal. If any of the checks fail the user will be brought back to the basket with some error messages.

The bit I want help with is the call to the function with the checks.

When a user clicks the "Buy Now" button can I call a function within the same page before the post fires? Or should I post to another page that runs the checks and then automatically posts the original form data to Paypal. Not sure if automatically posting forms is even possible? (To clarify, I should say that the user needs to be taken with the post data to Paypal)

metaBaron
  • 39
  • 9

2 Answers2

1

You can try using AJAX for the checks, but if an user deactivates Javascript, these checks will not be executed.

So if you use AJAX, you should put the redirection to paypal in AJAX.

But it's also possible to submit this form to a local page that verifies the data and then sends the data to paypal. This will not require Javascript.

Maarkoize
  • 2,601
  • 2
  • 16
  • 34
  • Thanks Marcel. Can you explain how this would work? "But it's also possible to submit this form to a local page that verifies the data and then sends the data to paypal. This will not require Javascript." – metaBaron Nov 19 '13 at 10:04
  • ...eg how do you send form data without JavaScript pressing the button for you? – metaBaron Nov 19 '13 at 10:05
  • You can just submit this form to e.g. `filter.php` and `filter.php` redirects then with the `header()` function to paypal. But then all POST data will be loosed. If paypal requires this POST data, you have to use AJAX. With AJAX you then can perform checks with a PHP function. – Maarkoize Nov 19 '13 at 10:08
  • The other page most submit the post data. This won't work without the form data being submitted to Paypal. How can i post form data automatically? – metaBaron Nov 19 '13 at 10:15
  • Maybe you can try to use CURL to send POST data from a php script to an external page. [look here](http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php) or [here](http://davidwalsh.name/curl-post) – Maarkoize Nov 19 '13 at 10:18
  • 1
    good one marcel. I just found similar CURL article too http://stackoverflow.com/questions/2835437/is-it-possilbe-to-automatically-submit-a-php-form – metaBaron Nov 19 '13 at 10:41
  • Hi Marcel, as far as I can work out cURL can post data on to a form but its not going to actually take the user to the form with the post data. Can you tell me how you thought this would be a solution? The user needs to be taken to Paypal with the post data that is passed from the form. – metaBaron Nov 19 '13 at 13:04
  • I don't unterstand the problem. You should submit the form data to an local php file where you can access the values with `$_POST['fieldname']`. Then you should be able to send this data with CURL with POST to paypal. For more help see my links above in my comment. – Maarkoize Nov 19 '13 at 13:52
  • Hi Marcel, ok bare with me. I am new to cURL. My question is, once the user presses the "Buy Now" button, and gets taken to the local php file that i will use to run my final validation functions and the cURL function, will the user be taken to the Paypal page [in the browser] along with the data we have posted? or will the cURL command simple pass data to the Paypal form? I think it's the later. I don't think cURL navigates the user to the Paypal page. – metaBaron Nov 19 '13 at 14:55
  • And i am new to Paypal ;) How does it work? Is there a form on the paypal webpage or local in your file which has it's action set to paypal? Maybe you should just try it und look, if it works. But you should take a look at this site [here](http://davidwalsh.name/curl-post) which shows exactly the thing you search for (I think ;)) Maybe this [site](http://www.electrictoolbox.com/php-curl-form-post/) is also helpful. – Maarkoize Nov 20 '13 at 07:42
  • Hi Marcel, When the person is in the basket on my site, they click "Buy Now". This takes them to the Paypal 'merchant gateway' [the same thing that happens when you pay for stuff on eBay]. What i want to happen is for a function to be fired when they click the "Buy Now" button that runs before they are taken to the Paypal page. I dont think this is possible in PHP alone. What i will probably do is take them to another 'confirm payment' page running my function on page load and having all there variables from the last page 'post' passed threw again, but 'hidden'. cURL won't do this.... – metaBaron Nov 20 '13 at 18:42
  • And is it necessary that this data is send via POST? Have you tried it with GET? Then you could use just the `header()` function to submit data to a new page. Maybe [this (answer)](http://stackoverflow.com/questions/14843212/submit-form-via-curl-and-redirect-browser-to-paypal) can help you. – Maarkoize Nov 21 '13 at 07:54
0

ajaxStart is what you are looking for.

From the official documentation:
Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
  • Thanks for getting back. I do however want a PHP answer. – metaBaron Nov 19 '13 at 09:59
  • @metaBaron: Yes. I have answered accordingly. You can fire ajax call to call a function in PHP which will perform all final checks. Because after all, user will click button on front-end. So, thats the best place to trigger something. – Bhavik Shah Nov 19 '13 at 10:02
  • I am trying to find an answer that doesn't use JavaScript or Ajax – metaBaron Nov 19 '13 at 10:09