<script type="text/javascript">
$(function(){
//Getting values from session and saving in javascript variable.
// But this will be executed only at document.ready.
var firstName = '<%= Session["FirstName"] ?? "" %>';
var lastName = '<%= Session["LastName"] ?? "" %>';
$("#FirstName").val(firstName);
$("#LastName").val(lastName);
$('Button').click(function(){
//Posting values to save in session
$.post(document.URL+'?mode=ajax',
{'FirstName':$("#FirstName").val(),
'LastName':$("#LastName").val()
} );
});
});
At Server side,
protected void Page_Load(object sender, EventArgs e)
{
if(Request.QueryString["mode"] != null && Request.QueryString["mode"] == "ajax")
{
//Saving the variables in session. Variables are posted by ajax.
Session["FirstName"] = Request.Form["FirstName"] ?? "";
Session["LastName"] = Request.Form["LastName"] ?? "";
}
}
For getting session values, as said by Shekhar and Rajeev
var firstName = '<%= Session["FirstName"] ?? "" %>';
I was looking for solution to this problem, until i found a very simple solution for accessing the session variables, assuming you use a .php file on server side. Hope it answers part of the question :
access session value
<script type="text/javascript">
var value = <?php echo $_SESSION['key']; ?>;
console.log(value);
</script>
Edit : better example below, which you can try on phpfiddle.org, at the "codespace" tab
You can't set session side session variables from Javascript. If you want to do this you need to create an AJAXPOST to update this on the server though if the selection of a car is a major event it might just be easier to POST this.
This is a cheat, but you can do this, send the value to sever side as a parameter
<script>
var myVar = "hello"
window.location.href = window.location.href.replace(/[\?#].*|$/, "?param=" + myVar); //Send the variable to the server side
</script>
if i keep it as it is , it will return same at all times even you loggedout. so while logging out i made
sessionStorage.setItem("name", "");
using this i emptied local variable. i used this process in single page website login/logout sessions. i hope this way will work for you because it worked for me. plz let me know your experiences.
To modify session data from the server after page creation you would need to use AJAX or even JQuery to get the job done. Both of them can make a connection to the server to modify session data and get returned data back from that connection.
<?php
session_start();
$_SESSION['usedData'] = "Some Value"; //setting for now since it doesn't exist
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Modifying PHP Session Data</title>
<script type='text/javascript'>
var usedData = '<?php echo $_SESSION['usedData']; ?>';
var oldDataValue = null;
/* If function used, sends new data from input field to the
server, then gets response from server if any. */
function modifySession () {
var newValue = document.getElementById("newValueData").value;
/* You could always check the newValue here before making
the request so you know if its set or needs filtered. */
var xhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
oldDataValue = usedData;
usedData = this.responseText; //response from php script
document.getElementById("sessionValue").innerHTML = usedData;
document.getElementById("sessionOldValue").innerHTML = oldDataValue;
}
};
xhttp.open("GET", "modifySession.php?newData="+newValue, true);
xhttp.send();
}
</script>
</head>
<body>
<h1><p>Modify Session</p></h1>
Current Value: <div id='sessionValue' style='display:inline'><?php echo $_SESSION['usedData']; ?></div><br/>
Old Value: <div id='sessionOldValue' style='display:inline'><?php echo $_SESSION['usedData']; ?></div><br/>
New Value: <input type='text' id='newValueData' /><br/>
<button onClick='modifySession()'>Change Value</button>
</body>
</html>
Now we need to make a small php script called modifySession.php to make changes to session data and post data back if necessary.
<?php
session_start();
$_SESSION['usedData'] = $_GET['newData']; //filter if necessary
echo $_SESSION['usedData']; // Post results back if necessary
?>
This should achieve the desired results you are looking for by modifying the session via server side using PHP/AJAX.
Possibly some mileage with this approach.
This seems to get the date back to a session variable. The string it returns displays the javascript date but when I try to manipulate the string it displays the javascript code.
ob_start();
?>
<script type="text/javascript">
var d = new Date();
document.write(d);
</script>
<?
$_SESSION["date"] = ob_get_contents();
ob_end_clean();
echo $_SESSION["date"]; // displays the date
echo substr($_SESSION["date"],28);
// displays 'script"> var d = new Date(); document.write(d);'