Ja du har sådanset ret, men jeg har haft problemer med at requesten ikke bliver færdighåndteret før siden skifter. Du kan selv prøve:
<?php
session_start();
$a = (isset($_SESSION["a"])) ? $_SESSION["a"] : "";
$b = (isset($_SESSION["b"])) ? $_SESSION["b"] : "";
$c = (isset($_SESSION["c"])) ? $_SESSION["c"] : "";
$d = (isset($_SESSION["d"])) ? $_SESSION["d"] : "";
$e = (isset($_SESSION["e"])) ? $_SESSION["e"] : "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="
http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Page 1</title>
<script type="text/javascript">
var active_element;
window.onload = function() {
var felter = document.getElementById("formular").elements;
for (i=0; i<felter.length; i++)
{
var elem = felter[i];
elem.onfocus=function() {
active_element=this;
};
elem.onblur=function() {
//saveValue(this,true);
};
}
};
//window.onbeforeunload = function(){
// if (active_element&&active_element.value&&active_element.name) {
// saveValue(active_element,false);
// }
//};
function go() {
//window.onbeforeunload = function(){
var felter = document.getElementById("formular").elements;
var qstring = "?";
for (i=0; i<felter.length; i++)
{
qstring += felter[i].name+"="+felter[i].value;
if (i+1<felter.length) {qstring+="&"}
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById('stat').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","savesessionvar.php"+qstring,true);
xmlhttp.send();
};
function saveValue(felt,sync) {
var qstring = "?"+felt.name+"="+felt.value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById('stat').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","savesessionvar.php"+qstring,sync);
xmlhttp.send();
}
</script>
</head>
<body>
<div>
<div>
<a href="form.php">Form</a> |
<a href="otherpage.php">Other page</a>
<input type="button" onclick="go();" />
</div>
<div id="lort">
<form action="#" id="formular">
<input type="text" value="<?php echo $a ?>" name="a" />
<input type="text" value="<?php echo $b ?>" name="b" />
<input type="text" value="<?php echo $c ?>" name="c" />
<input type="text" value="<?php echo $d ?>" name="d" />
<input type="text" value="<?php echo $e ?>" name="e" />
<input type="submit" />
</form>
</div>
</div>
</body>
</html>
og xhr ressourcen:
<?php
session_start();
foreach ($_REQUEST as $key => $value)
{
$_SESSION[$key] = $value;
}
?>