代码正在运行,由于某些原因我不知道,它已停止工作。
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文件没有被激活(我也试过了('更改')。
编辑:选中/取消选中复选框并返回1时,两个alert($('#hideFriendsPublicView').length)
警报都会被触发。
编辑:我也尝试过最初只在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在这些字符串上强制转换(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']
));
}
但那也失败了。
答案 0 :(得分:0)
认为您的owner_id可能导致问题是string
我不确定您的用户ID是字符串还是整数。如果是字符串,则应使用"
对其进行扭曲。如果你不认为javascript会认为是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']; ?>" });
}