ori
ori

Reputation: 43

javaScript fire php file and dont wait for reply

I am writing some accommodation web-site.I am using PayPal as billing system.Before I redirect the client to the PayPal page I need to insert all relevant data to the MySql database.

My question is:how do I fire the php( from the javaScript) without waiting to it´s replay?( and that the fact that the page that fire the php will no longer exist will make no problem)

Upvotes: 1

Views: 272

Answers (3)

Amarghosh
Amarghosh

Reputation: 59461

Use AJAX. Send an XMLHttpRequest to the appropriate php script with relevant data and redirect the client upon receiving successful response back from the script.

Upvotes: 0

Pekka
Pekka

Reputation: 449713

If it has to be JavaScript, I would make this a normal AJAX operation (See here for JQuery based Ajax.)

I find it dangerous, though, to start a request and not wait for the reply. What if the user's network is congested, and the request has to wait on the client end, and never makes it out before the user proceeds to the next page? Better wait for the success event.

Using JavaScript for this has the massive downside that if JavaScript is turned off, or a JavaScript error occurs, vital data will not be written into your database, while the shopping procedure may go on as planned. Check whether this is really what you want. The header based approach by Cletus is definitely the safest.

Upvotes: 2

cletus
cletus

Reputation: 625307

If you need to insert data into the database and redirect to a Paypal page you only really have one choice: post the form to the server, write the data to the database and then send a redirect back to the client. So:

<?php
// save data to database
header('Location: http://www.paypal.com');
exit;

Upvotes: 3

Related Questions