Jump to content

Archived

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

jckrueger

pass Javascript data to PHP $variable

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

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
i don't know for 100%, but i think the whole purpose of ajax is to pass things live from client to server and back. but i haven't gotten around to looking into ajax yet, so i couldn't tell you for sure.

Share this post


Link to post
Share on other sites
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\" /]

Share this post


Link to post
Share on other sites

×

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.