该代码正在运行,但由于某种我不知道的原因,它停止了工作。
jQuery(在 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']
));
}
似乎使用相同的确切代码,单击复选框时 ajax.php 文件被启动,并且 MySQL 也得到更新。
现在,单击时 PHP 文件不会被触发(我也尝试过('change')。
编辑:alert($('#hideFriendsPublicView').length)
选中/取消选中复选框并返回 1 时,两个警报都会被触发。
编辑:我最初也尝试在 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']; ?>"});
}
});
并且 PHP 在这些字符串上转换 int(2 列的类型是 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']
));
}
但这也失败了。