I have a series of checkboxes. When a user “checks” a box it updates the database on the fly. This works great in Firefox, but in IE8 I am getting some strange behavior.
In IE8, I can “check” a box and it updates fine. I can “uncheck” the same box and it works fine, but if I try again, it does not update the database. I am thinking that it may have something to do with the “Microsoft.XMLHTTP” part of the code but I have no idea how to correct the problem. Any help is greatly appreciated. Thanks.
Here is the code:
Javascript:
function chkit(uid, chk) {
chk = (chk==true ? "1" : "0");
var url = "check_validate.php?userid="+uid+"&chkYesNo="+chk;
if(window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
// Use get instead of post.
req.open("GET", url, true);
req.send(null);
}
PHP:
// Get the variables.
$userid = $_GET['userid'];
$chkYesNo = $_GET['chkYesNo'];
$sql = "
UPDATE vfe_lots
SET sold ='$chkYesNo'
WHERE id = '$userid'
";
mysql_query($sql) or die(mysql_error());
HTML & PHP:
<form action="#" method="get">
<table width="50%" cellpadding="3">
<?php
include("dbconn.php");
$query="select * from vfe_lots ";
$result = mysql_query($query);
$counter = 0;
$cells_per_row = 2;
while($row=mysql_fetch_array($result))
{
$counter++;
if(($counter % $cells_per_row) == 1) { echo '<tr style="background-color: #e3e3ce">'; }
echo '<td>Lot '.$row['id'].'</td><td><input name="chk" type="checkbox" id="chk_'.$row['id'].'" value="'.$row['id'].'" onclick="chkit('.$row['id'].', this.checked);" ';
if ( $row['sold'] == 1 ) {
echo "checked='yes'";
}
echo ' /></td>';
if(($counter % $cells_per_row) == 0) { echo '</tr>'; }
}
// just in case we haven't closed the last row
// this would happen if our result set isn't divisible by $cells_per_row
if(($counter % $cells_per_row) != 0) { echo '</tr>'; }
?>
</table>
</form>