Jump to content

pass Javascript data to PHP $variable


jckrueger

Recommended Posts

[img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Greetings!
I need to assign the value from a javascript var to a PHP $variable.

I have been banging my head for days trying to accomplish this task. There has got to be an easy way. Can anyone help me???
jckrueger

Link to comment
https://forums.phpfreaks.com/topic/10087-pass-javascript-data-to-php-variable/
Share on other sites

The short answer is that you can't. php only exists on the server and generates a file rendered by your client-side browser. javascript only exists on the client side. So the javascript variable will only exist after the php script has executed.

If you explain a little more, there's probably ways to accomplish what you want to do (just not the way you described).
[!--quoteo(post=375597:date=May 20 2006, 08:10 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 20 2006, 08:10 PM) [snapback]375597[/snapback][/div][div class=\'quotemain\'][!--quotec--]
The short answer is that you can't. php only exists on the server and generates a file rendered by your client-side browser. javascript only exists on the client side. So the javascript variable will only exist after the php script has executed.

If you explain a little more, there's probably ways to accomplish what you want to do (just not the way you described).
[/quote]

I think he wants to assign a javascript code to a var so he can print it certin places.
Yep, that's the whole point of ajax. It uses a predifined javascript class called XMLHttpRequest to exchange variables between a server side script and javascript. In IE the same class is available as build-in ActiveX object. This way you can change PARTS of the page, without refreshing the whole page.

Using Ajax (asynchronous javascript and xml) you can call a php script like this:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]http.open('get', 'phpscript.php?act='+act); [/quote]

As you can see I'm passing javascript var "act" to script "phpscript.php" by way of "get"!

Using:
[code]var response = http.responseText; [/code]
I can have all text returned by the serverside script loaded into a variable.

So it works both ways.


There's a bit more to it than that, so go to www.ajaxfreaks.com and check out the beginnertuts. You'll be passing variables before you know it! [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.