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)

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

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."
...eg how do you send form data without JavaScript pressing the button for you?
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.
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?
good one marcel. I just found similar CURL article too stackoverflow.com/questions/2835437/…
|
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.

3 Comments

Thanks for getting back. I do however want a PHP answer.
@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.
I am trying to find an answer that doesn't use JavaScript or Ajax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.