Click here to Skip to main content
14,733,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 16-Jul-12 21:06pm
v2

I can't remember the finer details of using sessions, or more specifily what their lifespan and visibility is.

But this could very simply be achieved with an ajax call and a php file. What I have in mind is something this (with error checking removed for simplicity):
setSessionVar.php
<?php
    $varName = $_GET['varName'];
    $value = $_GET['value'];
    $_session[$varName] = $value;
?>


Then some simple javascript that would construct a url in order to pass the data.

var name = 'enhzflep';
var url = 'setSesion.php?varName=name&value=' + name;

getAjaxRequest(url);


Where getAjaxRequest was a function that would perform an ajax call to the specified url but do nothing when the call was complete - or at least dislay nothing if all was well. You could have the php send back a status message that you then check for before continuing.

Everyone's using jQuery these days, though you can write the function yourself or use a purpose built ajax library in well under 4kb - or about 4.5% of the size of jquery.min.js

For reference, I use as compressed version of this: AjaxRequest[^]
   
Comments
jkirkerx 18-Jul-12 22:50pm
   
That's a good idea

Before I posted my Javascript question, I was reading that you can register a php script as javascript, and php would run it anyways not knowing the difference.
hi,


you can use the hidden filed for the same .
<input id="hiddenfield1" type="hidden" value="something" runat="server"></input>


value can be assige dynamically
   
Comments
enhzflep 17-Jul-12 20:01pm
   
Doesn't work for php.
I read somewhere that javascript is capable of running php scripts a couple nights ago,

I googled advanced javascript and ran across the subject.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900