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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.