I have check boxes that get generated dynamically, when the check box is checked the onchange method works fine and my function is called also when the box is unchecked by user the onchange method calls my intended function the problem is when the box is checked a part of the page becomes active which is good but when the user un-checks the checkbox the that part of the page becomes active again, and the value of the checkbox "checked " stays yes after the first time it was checked. Is there way to detect when the user choose to un-check so the active part of the page becomes inactive.
her is my code
echo '<table width="85%" border="1" cellpadding="0"; cellspacing="0" align="center">
<tr>
<th width="23%" height="44" scope="col" align="left"> '.$query_rows['categoryTopic'].' <br><br><br></th>
<th width="24%" scope="col">'.$Count.'</th>
<th width="25%" scope="col"> 0 </th>
<th width="28%" scope="col"> <form name = "choose" method="post" action="activateImages.php">
<input type="checkbox" value= "5" onChange="window.edit()" </form></th>
</tr>
</table>';
}
?>
Java code:
<script type="text/jscript">
//this funciton will be called when user checks a check box.
function edit(){
//get selected category from the form
//var formName = 'choose';
//var Category = document[formName]['choose'].value;
//check if browser suports ajax
var xmlhttp = null;
if(typeof XMLHttpRequest != 'udefined'){
xmlhttp = new XMLHttpRequest();
}
else if(typeof ActiveXObject != 'undefined'){
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
else
throw new Error('You browser doesn\'t support ajax');
//open connection with activateImages.php to recieve the active images as an acho
xmlhttp.open("GET", "activateImages.php",true);
//echeck if ready to recieve
xmlhttp.onreadystatechange = function (){
if(xmlhttp.readyState == 4)
window.activate(xmlhttp);
};
xmlhttp.send(null);
}
//recieve the active images then insert them in the specified location of the page.
function activate(xhr){
if(xhr.status == 200){
document.getElementById('images').innerHTML = xhr.responseText;
}
else
throw new Error('Server has encountered an error
'+
'Error code = '+xhr.status);
}
</script>
An un-checked checkbox does not get sent to the server when a form is posted. So unless you are explicitly sending an empty value in your javascript, you should not check for the value, but whether it is set:
if (isset($_POST['checkbox'])) {
Apart from that there are several errors in this mix of code and pseudo-code, like the un-closed html tags, missing name attribute for the checkbox, assignment instead of comparison, etc.
Edit: It also seems you are not actually sending anything to the server and you are mixing GET (javascript) POST (removed php code).