Jump to content

transferring javascript variable to php variable within the script


kaelwithme

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

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.