简体   繁体   中英

jQuery $.post on checkbox change not working

The code was working, and for some reason I don't know, it stopped working.

jQuery (inside document.ready):

$('#hideFriendsPublicView').on('click', function() {
            if ($(this).is(':checked')) {
                alert($('#hideFriendsPublicView').length);
                $.post("ajax.php", { checked: 1, owner_id:<?php echo $profileData[0]['id']; ?>}); //owner_id as an int, as the id in mysql is of type int too.
            } else {
                alert($('#hideFriendsPublicView').length);
                $.post("ajax.php", { checked: 0, owner_id:<?php echo $profileData[0]['id']; ?>});
            }
});

HTML:

if ( $profileData[0]['showFriends'] ) {
   echo '<label><input type="checkbox" checked="checked" id="hideFriendsPublicView"> Hide my friends from public view.</label>';
} else {
   echo '<label><input type="checkbox" id="hideFriendsPublicView"> Hide my friends from public view.</label>';
}

PHP:

if ( isset($_POST['checked']) ) {
    $query = $conn->prepare("UPDATE users SET showFriends = :newState WHERE id = :id");
    $query->execute(array(
        ':newState' => $_POST['checked'],
        ':id' => $_POST['owner_id']
    ));
}

It seems like with this same exact code, the ajax.php file was getting fired up when clicking the check box, and MySQL was also getting updated.

Now, the PHP file doesn't get fired up when clicking (I also tried on('change').

Edit: both of the alert($('#hideFriendsPublicView').length) alerts are getting fired when checking/unchecking the checkbox and return 1.

Edit: I also tried using only strings initially in the jQuery, IE:

    $('#hideFriendsPublicView').on('click', function() {

        if ($(this).is(':checked')) {
            alert($('#hideFriendsPublicView').length);
            $.post("ajax.php", { checked: "checked", owner_id:"<?php echo $profileData[0]['id']; ?>"});
        } else {
            alert($('#hideFriendsPublicView').length);
            $.post("ajax.php", { checked: "unchecked", owner_id:"<?php echo $profileData[0]['id']; ?>"});
        }
    });

And the PHP to cast ints on these strings (the type of the 2 columns is int):

if ( isset($_POST['checked']) ) {
    if ( $_POST['checked'] == "checked" ) {
        $_POST['checked'] = 1;
    } else {
        $_POST['checked'] = 0;
    }
    $query = $conn->prepare("UPDATE tusers SET profile_showFriends = :newState WHERE id = :id");
    $query->execute(array(
        ':newState' => $_POST['checked'],
        ':id' => (int) $_POST['owner_id']
    ));
}

But that also fails.

Think the problem may cause by your owner_id is a string

I not sure your user id is string or integer. if it string you should warp it up with " . if you dont javascript will think is a variable

if ($(this).is(':checked')) {
                alert("bla");
                $.post("ajax.php", { checked: 1, owner_id:"<?php echo $profileData[0]['id']; ?>" });
            } else {
                alert("bla");
                $.post("ajax.php", { checked: 0, owner_id:"<?php echo $profileData[0]['id']; ?>" });
            }

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