<?php
			session_start();

//Why does the checkbox stay checked when reloading the page?
//<META HTTP-EQUIV="Pragma" CONTENT="no-cache"> 

// tableau associatif

			$etat_connexion = array("checkbox1" => "true", "checkbox2" => "true", "checkbox3" => "true");
			//$etat_connexion = array();
			$_SESSION['etat_connexion'] = $etat_connexion;
			
			echo "Start ...";
			
?>


<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <meta http-equiv="pragma" content="no-cache" />
</head>

<script>


function getXhr()
{
			var xhr = null; 
			if(window.XMLHttpRequest) // Firefox et autres
			   xhr = new XMLHttpRequest(); 
			else if(window.ActiveXObject)
			{ // Internet Explorer 
			   try 
			   {
					xhr = new ActiveXObject("Msxml2.XMLHTTP");
			   } 
			   catch (e) 
			   {
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
			   }
			}
			else 
			{ 
				// XMLHttpRequest non supporté par le navigateur 
				alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
				xhr = false; 
			} 

			return xhr;
}

function action(id)
{
	//alert(id);
	//document.getElementById(nbCheck).checked == true
	
	var etat = document.getElementById(id).checked;
	//alert(etat);
	ajax(id,etat);
}


function ajax(id,etat)
{
			var xhr = getXhr();
								
			xhr.onreadystatechange = function()
			{
				// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
				if(xhr.readyState == 4 && xhr.status == 200)
				{
					retour = xhr.responseText;
					alert(retour);
				}
			}

			xhr.open("POST","sauvegarde_session.php",true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					
			
			
			xhr.send("id="+id+"&etat="+etat);
			
			//alert(id + etat);


					
}
</script>

<?php

			$cpt = 1;

			foreach($etat_connexion as $x => $valeur) 
			{
				$id_str = $x; //"checkbox" .$cpt;
				$name_str = $id_str;
				$valeur_str = $cpt;
				
				if($valeur == "true")
				{
					echo "<input id='$id_str'  type='checkbox' name='$name_str' value='$valeur_str' checked='checked' onclick='action(this.id)'>";
				}
				
				else
				{
					echo "<input id='$id_str'  type='checkbox' name='$name_str' value='$valeur_str' onclick='action(this.id)'>";
				}
				$cpt = $cpt + 1;	
			}


?>

<!--
<input id="checkbox1"  type="checkbox" name="checkbox1" value="1" checked="checked">
<input id="checkbox2" type="checkbox" name="checkbox2" value="2">
<input id="checkbox3" type="checkbox" name="checkbox3" value="3" onclick="action(this.id)">
-->
</html>