Can we send javascript variables to as server side variable?

hi,
can we send javascript variables to as server side variable
i have JavaScript variable i want to send as php session value ,
like(var name=“first name”;
i want to send this name to
$_session[‘name’]=name;
)
is it possible ?
if possible how it is?
Thank u

2 options are to use ajax or just append the variable name and value as a query string to the url of the php script that sets the session variable.

There are other options as well but unless you are more specific in exactly what you want to do not all the options would be suitable.

You can also send a variable as a hidden field in a form.


// place holder in your html form
<input type=hidden name=username value="" />


// Shunt a value to the html element using JS
oFormObject.elements["username"].value = 'Jon Lord';

The only way you can do this is by

  • using a form to $_POST the values to the server
  • using ajax to send the data and store it

Apart from those 2 basic ways there is no way JavaScript can communicate with PHP variables directly.

Thank u for your response… i am using like that only…

changeScript(“/index.php?name=”+name);

I can’t believe my answer was deleted and I was accused of thread highjacking! Below is a totally viable method to send javascript variable to any server-side language without the need to use third party libraries like ajax or jquery!

[COLOR=#2968a1]http://www.sitepoint.com/forums/show…script-Refresh[/COLOR]

Javascript:

var name="anonymous"
changeScript("/index.php?name="+name);

Server coded Javascript: (/index.php)

name=$_GET["name"];
$_SESSION["name"]=name;

header("ContentType: application/x-javascript");
header("Cache-Control: no-cache");
header("Expires: -1");

echo "alert(\\"".$_SESSION["name"]."\\");";

Sorry, haven’t been here for a long while. Missed a few “$”'s on the PHP part of the post above and the edit button has gone. I guess you can only edit a few times. Make sure you put them in for it to work. ($name not name)