View Full Version : How to pass a javascript variable value to a PHP variable without refreshing the page

01-20-2012, 02:55 PM
Hi, experts, I want to pass a javascript variable value to a PHP variable at the same page without refreshing the web page, how could I do it?

For your reference, here is a part of coding:


function leftarrow(){
var e = event.srcElement;
var kname = e.name;
var key = kname.substr(3, kname.length-1);


I want to pass the value of 'key' and 'direct' to PHP variables.

Thanks for your help.

01-20-2012, 02:56 PM
You need to look up ajax which can run an invisible http request for you.

01-21-2012, 05:13 AM
Dear Tangoforce,

Thank you very much for your quick response.

I ever used Ajax to check whether a member's ID and password are right when he login. Generally, through javascript 'getDocumentById' to the values of ID and password, and through Ajax XMLHttpRequest() transfer to a PHP file to check their availabilities in the database, and 'responseText' to get the feedback message from the PHP file. The message will be presented in a textfield by Javascript.

However, I never use the ajax to transfer the vaules of the variables back to some variables at the same PHP page.

Could you kindly provide some detail steps on how to transfer the values to the variables back to a PHP variable in the same PHP page?

Thank you very much for your help.

01-21-2012, 04:27 PM
Dear Tangoforce,

I think your suggestion over and agian.

Maybe, you mean, I write javascript and ajax coding in a php file (for example, index.php), and also use this PHP file (index.php) as the receiver of the data transferred by XMLHTTPRequest(), and the feedback message sent by the same PHP file (index.php) will be put into a textfiled by, for instance, msg.innerHTML=.., but the feedback message will be set as 'hidden'.

Does it make sense?

Thank you.

01-21-2012, 06:33 PM
Does it make sense?

No not really.

You've asked how to pass javascript variables to PHP without refreshing the page. The only way to do that is by using javascript and using ajax / xmlhttp.

That is the only way.

AJAX works just like the browser - it sends and receives data by get or post just like a normal http call. Therefore you write your php in the same way as you would for any normal page.