我一直在研究Like和不同功能与jQuery,ajax和PHP。我的问题有点难以理解。让我们先试着理解它。 Jquery Post无法正常工作?
我有2个PHP页面,
viewProfile.php
和
LikeMail.php
。
LikeMail.php
正在通过
viewProfile.php
中的ajax函数调用。
这里是viewProfile.php页的说明部分
-----------------
| Like/Unlike |
-----------------
这里是按钮,它实际上是由这个AJAX功能
function like()
{
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
if(req.readyState==4 && req.status==200)
{
document.getElementById('Like').innerHTML=req.responseText;
}
}
req.open('POST','LikeMail.php','true');
req.send();
}
setInterval(function(){like()},1000);
HTML来自LikeMail.php
:
<div id="Like"></div>
输出在此处显示在此div中。 上面的按钮可能是Like
或Unlike
取决于LikeMail.php
中的条件,这将在以下LikeMail.php
说明部分中进行描述。
当他们中的一个(按钮)Like
或Unlike
被点击时。然后它调用各自的jquery点击功能,发送post请求到LikeMail.php
。我已经提到Indirect
页面的标题,因为Like
或Unlike
按钮实际上存在于LikeMail.php
页面。但由于ajax调用这些按钮正在显示在viewProfile.php
页面。所以,我再通过viewProfile.php
发送POST请求到实际的页面LikeMail.php
这是jQuery的职位Unlike
按钮
$(document).ready(function(){
$('#Unlike').unbind().click(function(){
$.post("LikeMail.php",
{Unlike: this.id},
function(data){
$('#response').html(data);
}
);
});
});
这是jQuery的岗位或Like
按钮
$(document).ready(function(){
$('#Like').unbind().click(function(){
$.post("LikeMail.php",
{Like: this.id},
function(data){
$('#response').html(data);
}
);
});
});
描述的结束部分viewProfile.php
页面
这里是LikeMail.php页的描述
Like
或
Unlike
按钮被
viewProfile.php
页所示的截面取决于此代码:
$check_for_likes = mysqli_query($conn, "SELECT * FROM liked WHERE user1='$user1' AND user2='$user2'");
$numrows_likes = mysqli_num_rows($check_for_likes);
if (false == $numrows_likes) {
echo mysqli_error($conn);
}
if ($numrows_likes >= 1) {
echo '<input type="submit" name="Unlike" value="Unlike" id="Unlike" class="btn btn-lg btn-info edit">';
}
elseif ($numrows_likes == 0) {
echo '<input type="submit" name="Like" value="Like" id="Like" class="btn btn-lg btn-info edit">';
}
按钮取决于上述这两个条件。
现在,当点击Like
按钮,从viewProfile.php
post请求来这里
if(isset($_POST['Like'])) //When Like button in viewProfile.php is clicked then this peace of code inside if condition should run and insert some record in database
{
$total_likes = $total_likes+1;
$like = mysqli_query($conn, "UPDATE user SET user_Likes = '$total_likes' WHERE user_id = '$user2'");
$user_likes = mysqli_query($conn, "INSERT INTO liked (user1,user2) VALUES ('$user1','$user2')");
$query3 = "INSERT INTO notification (user1, user2, alert, notificationType) VALUE ('$user1','$user2','unchecked','like')";
if (mysqli_query($conn, $query3)) {
echo "Like";
} else {
echo mysqli_error($conn);
}
}
同样,当点击Unlike
按钮。代码的和平应该运行。
if(isset($_POST['Unlike'])) //This is the condition for Unlike button. It should delete record from databse
{
$total_likes = $total_likes-2;
$like = mysqli_query($conn, "UPDATE user SET user_Likes='$total_likes' WHERE user_id='$user2'");
$remove_user = mysqli_query($conn, "DELETE FROM liked WHERE user1='$user1' AND user2='$user2'");
$query3 = "DELETE FROM notification WHERE user1='$user1' AND user2='$user2' AND notificationType='like'";
$check = mysqli_query($conn, $query3);
if ($check) {
echo "Unlike";
} else {
echo mysqli_error($conn);
}
}
问题:
我所面临的主要问题是,当我点击Like
或Unlike
二者执行的Like
按钮的代码的条件。两者都将数据插入数据库中作为Unlike
条件应该从数据库中删除数据,但它也会按照Like
按钮的条件插入数据。请你帮我解决这个问题。提前致谢!
更新:
当我删除Like
按钮的所有代码。 Unlike
按钮的条件开始正常工作。
总结到人们可以轻松地集中主要问题 – kollein
@kollein综述后'Problem'标题 –
需要跟踪您的变量来描述检查它们是否损坏,或者最终状况是否有误。在PHP端使用和滥用'print_r()'和'error_log()',在javascript上使用'alert()'或'console.log()'。 – technico