jckrueger Posted May 20, 2006 Share Posted May 20, 2006 [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 Quote Link to comment https://forums.phpfreaks.com/topic/10087-pass-javascript-data-to-php-variable/ Share on other sites More sharing options...
AndyB Posted May 21, 2006 Share Posted May 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/10087-pass-javascript-data-to-php-variable/#findComment-37520 Share on other sites More sharing options...
flamerail Posted May 21, 2006 Share Posted May 21, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/10087-pass-javascript-data-to-php-variable/#findComment-37545 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/10087-pass-javascript-data-to-php-variable/#findComment-37548 Share on other sites More sharing options...
448191 Posted May 21, 2006 Share Posted May 21, 2006 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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/10087-pass-javascript-data-to-php-variable/#findComment-37597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.