简体   繁体   中英

How do I get the value of a checkbox on click

I have an html table with a checkbox created dynamically on each row. The checked status of the checkbox is set based on a value in the database. When the checkbox is checked in the html table, I need to catch the change and set the value so that if the box is checked, value=1 else value=0. Value is passed to the update query. I'm not sure how to accomplish this with my current code.

HTML:

<?php 
    $sqlGetEmployees = "select e.employeeID, e.lastName, e.firstName, l.number, p.name, e.gradeCertLevel, e.serviceYears, e.step, e.certified from employee e join location l on l.locationID = e.locationID join position p on p.positionID = e.positionID";
    $stmtGetEmployees = mysqli_prepare($db,$sqlGetEmployees);
    mysqli_stmt_execute($stmtGetEmployees);
    mysqli_stmt_bind_result($stmtGetEmployees, $id, $lastName, $firstName, $number, $name, $gradeCertLevel, $serviceYears, $salaryStep, $certified);

    $count = 1; 

   while(mysqli_stmt_fetch($stmtGetEmployees))
   {
        $checkedCertified = ($certified == '1') ? 'checked="checked"':'';
?>
    <tr>
        <!-- other table data -->
        <td> 
            <div id='certified_<?php echo $id; ?>'>
                <input contentEditable='true' class='edit' type='checkbox' id='certified' <?php echo $checkedCertified; ?> />
            </div> 
        </td>
    </tr>
<?php
        $count ++;
    }
    mysqli_stmt_close($stmtGetEmployees);
?>  

Script:

    // Add Class
    $('.edit').click(function(){
        $(this).addClass('editMode');

    });

    // Save data
    $(".edit").focusout(function(){
        $(this).removeClass("editMode");

        var id = this.id;
        var split_id = id.split("_");
        var field_name = split_id[0];
        var edit_id = split_id[1];

        var value = $(this).text();

        $.ajax({
            url: 'update.php',
            type: 'post',
            data: { field:field_name, value:value, id:edit_id },
            success:function(response){
                console.log('Save successfully');               
            }
        });

    });

update PHP

$field = $_POST['field'];
$value = $_POST['value'];
$editid = $_POST['id'];
$checkedStatus = $_POST['checked'];

if($field == "certified")
{
    $sqlUpdateCertified = "UPDATE employee SET ".$field."=? WHERE employeeID= ?";
    $stmtUpdateCertified = mysqli_prepare($db,$sqlUpdateCertified);
    mysqli_stmt_bind_param($stmtUpdateCertified, "ss",$checkedStatus,$editid);
    mysqli_stmt_execute($stmtUpdateCertified);
    mysqli_stmt_close($stmtUpdateCertified);
}

For dynamically created elements events are handled like this:

$(document).on('click', '.edit', function(e) {
   //e.preventDefault();
   var checkedStatus = 0;
   if ($(this).is(':checked')) {
       $(this).addClass('editMode');
       checkedStatus = 1;
   } else {
       checkedStatus = 0;
       $(this).removeClass("editMode");
   }  

    var id = this.id;
    var split_id = id.split("_");
    var field_name = split_id[0];
    var edit_id = split_id[1];

    var value = $(this).text();

    $.ajax({
        url: 'update.php',
        type: 'post',
        data: { field:field_name, value:value, id:edit_id, checked: checkedStatus },
        success:function(response){
            console.log('Save successfully');               
        }
    });
});

To put everything in a single event handler you can either put everything in .click or .focusout depending on what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM