kaelwithme Posted January 17, 2011 Share Posted January 17, 2011 hi, my code looks like this. <script> var var1 = '1'; document.write("<?php $var2 = ; ?>"); </script> the [code missing] part is where i think i should put in the value of var1 into the php $var2 in a way that i can still use the php $var2 in other php codes in the page.. i want to do this with out the $_GET functions. just within the script. can you guys help me with this?. thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/ Share on other sites More sharing options...
trq Posted January 17, 2011 Share Posted January 17, 2011 Javascript executes on the client, while php, on the server. The only way to get a variable from the client, back to the server is to make another request. This can either be done by making a new http request and refreshing the entire page or via an Ajax call. Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1160518 Share on other sites More sharing options...
kaelwithme Posted January 17, 2011 Author Share Posted January 17, 2011 so i can't actually tweak the script up just to make the javascript variable go inside the php variable on the fly? php variable to javascript variable is alright as i have already tried it. but vice versa, i dunno. i have no idea.. i tried the said code above but it doesn't return any value. do you know of any way as long as the page doesn't reload? thank you Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1160522 Share on other sites More sharing options...
haku Posted January 17, 2011 Share Posted January 17, 2011 Let me rephrase Thorpe's answer, to make it a little bit easier to understand, since you didn't seem to get it: It can't be done. Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1160530 Share on other sites More sharing options...
kaelwithme Posted January 17, 2011 Author Share Posted January 17, 2011 crap.. i was hoping to get a little light. :-\ anyway.. can i do it via ajax without visually reloading the page. Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1160561 Share on other sites More sharing options...
haku Posted January 17, 2011 Share Posted January 17, 2011 It depends on what you are trying to do. You mentioned using a javascript variable to set a variable in PHP, and output the rest of the page with that variable being available. That you can't do even if you were to send it by AJAX, as the page would be already loaded. However, if you want to take some data on the page, and use AJAX to get some data from the server, which you will then use to alter the page, you can do this. Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1160568 Share on other sites More sharing options...
RIRedinPA Posted January 17, 2011 Share Posted January 17, 2011 I've done this before. Build a page in PHP that sets and gets session variables. Then build a function in Javascript similar to this: function manageSessionData(process, datafield, fieldvalue) { //declare returnvalue var var returnvalue; //set the querystring if(process == "set") { var querystring = "process=" + process + "&datafield=" + datafield + "&" + datafield + "=" + fieldvalue; } else { var querystring = "process=" + process + "&datafield=" + datafield; } $.ajax({ type: "POST", data: querystring, url: "lib/includes/manageSessionData.php", success: function(data){ returnvalue = data; }, async: false }); if(process == "get") { return returnvalue; } } then whenever I want to set or get a session var within JS I just call the function: //get session value for usertype var usertype = manageSessionData("get", "usertype", "null"); then you could use jquery or hard code or some other framework to insert the value into an element... $('#divelement').html(usertype); But like others have replied, going from JS directly to PHP is a closed street. You need to work around it. Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1160888 Share on other sites More sharing options...
kaelwithme Posted January 20, 2011 Author Share Posted January 20, 2011 thanks for the reply.. currently, the issue is on hold. since i'm doing something else. but if i get back on it, i'll try to do what you suggested. thank you. Quote Link to comment https://forums.phpfreaks.com/topic/224668-transferring-javascript-variable-to-php-variable-within-the-script/#findComment-1162366 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.