Jquery Post工作不正常?

时间:2016-08-19 13:04:45

标签: javascript php jquery ajax

我一直在使用jquery,ajax和php处理Like and Different功能。我的问题有点难以理解。让我们先尝试理解它。


我有2个php页面,viewProfile.phpLikeMail.phpLikeMail.php中的ajax函数正在调用viewProfile.php

以下是viewProfile.php页面描述的部分

                             -----------------
                            |   Like/Unlike   |
                             -----------------

这是一个按钮,它实际上来自这个ajax函数的LikeMail.php

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:

<div id="Like"></div>

此div中显示输出。 以上按钮可以是LikeUnlike取决于LikeMail.php中的条件,这将在LikeMail.php说明部分中进行说明。

当其中一个(按钮)LikeUnlike被点击时。然后它调用相应的jquery点击功能,向LikeMail.php发送帖子请求。我在标题中提到Indirect页面,因为LikeUnlike按钮实际存在于LikeMail.php中页。但由于ajax调用,这些按钮显示在viewProfile.php页面中。然后我通过viewProfile.php将帖子请求发送到实际页面LikeMail.php

这是Unlike按钮

的jquery帖子
$(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页面中显示UnlikeviewProfile.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的发布请求来到此处

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);
                }
            }


问题:
我遇到的主要问题是,当我点击LikeUnlike时,两者都执行Like按钮代码的条件。两者都将数据插入数据库,因为Unlike条件应该从数据库中删除数据,但它也会插入数据作为Like按钮的条件。请你帮我解决一下如何解决这个问题。提前致谢!

更新:
当我删除Like按钮的所有相应代码时。 Unlike按钮的条件开始正常工作。

1 个答案:

答案 0 :(得分:1)

我认为某处有重复的ID,也许是DIV。看看这个。

<div id="Like_2"></div>

<input type="submit" name="Unlike" value="Unlike" id="Unlike" class="btn btn-lg btn-info edit">

<input type="submit" name="Like" value="Like" id="Like" class="btn btn-lg btn-info edit">

<div id="response"></div>


$(document).ready(function(){
    $(document).on('click','#Unlike', function(){
        $('#response').html(this.id);
        //ajax call

    });
    $(document).on('click','#Like', function(){
        $('#response').html(this.id);
        //ajax call

    });
});

和javascript函数:

function like()
{
    var req  = new XMLHttpRequest();
    req.onreadystatechange = function()

    {
        if(req.readyState==4 && req.status==200)
        {
            document.getElementById('Like_2').innerHTML=req.responseText;

        }
    }
    req.open('POST','LikeMail.php','true');
    req.send();
}
setInterval(function(){like()},1000);

https://jsfiddle.net/wx38rz5L/2103/