I am using the following ajax javascript to pass values if checkbox is checked or not, The problem which I am facing is that it is working only for the first row from the mysql fetch aray and for the rest of the results it always sends checkboxstatus = 1 as a result, no matter if the chekbox is checkied or not. here is the code:
<script>
$(document).ready(function(e) {
$('input[name=status]').change(function(){
if( $('input[name=status]').prop('checked') )
{checkboxstatus = '1';}
else
{checkboxstatus = '0';}
var idm = $(this).attr('idm');
$.ajax({
type: "POST",
url: "checkboxtestbackend.php",
data: {"checkboxstatus": checkboxstatus,
"idm":idm
},
})
.fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);});
});//end change
});//end ready
</script>
Usualy all the checkgoxes are checked in the first sql fetch if I uncehck the checbox it POST's 0 as value , but for the rest of the sql fetch results it always sends "1" as a POST result no matter checked or not.
this is the code which shows the checkboxes: <?php echo "<input type='checkbox' idm='$idm' name='status'"; if($row['status'] == 1){print "checked='checked'"; }echo "/>";?>
The visualizations of the status of the checkboxes is working but the change it is not.
This is the sql query fot it:
$sql = "SELECT * FROM cursos ORDER BY id DESC";
if (!$result = mysqli_query($con,$sql))
{
die("Error: " . mysqli_error($con));
}
<?php
while($row = mysqli_fetch_array($result))
{
$idm = $row['id'];
?>
<td align="center"><?php echo "<input type='checkbox' idm='$idm' name='status'"; if($row['status'] == 1){print "checked='checked'"; }echo "/>";?></td>