ajax喜欢/不像按钮没有切换回来

时间:2012-11-02 00:34:31

标签: php jquery ajax

我正在尝试在页面上创建一个类似的按钮,似乎无法让它正常工作。基本上有三个函数使用ajax将数据发送到更新数据库的php页面。我已经检查了数据库,所有三个都正确更新。如果用户最初不喜欢并点击,它会正确显示不同的按钮但是,如果你点击不同它不会切换回来(虽然它会更新数据库)。

这是正确的设置方法吗?我对ajax很新,我不确定这是否是正确的方法。提前致谢 史蒂夫

public function likesScript($p){?>
    <script>

//display list of people who like this
    function getLikes(){
    $.ajax({
        type: "POST",
        url: "likelist.php",
        data: { p: "<?php echo $_GET['p']?>"}
    }).success(function(res) {


         //check to see if current user likes this   
        if($('li#<?PHP echo $_SESSION['userId']; ?>').length){
            $(".Like").addClass('hidden');

            $(".UnLike").removeClass('hidden');
        }
        else{  
            $(".UnLike").addClass('hidden');
            $(".Like").removeClass('hidden');

        }

        $("#likedBy").append(res); 
        console.log(res);

    });
}


function removeLike() {
    $.ajax({
        type: "POST",
        url: "likedata.php",
        data: { arg1: "<?php echo $_SESSION['userId']?>", arg2: "<?php echo $p;?>", arg3: "0" }
    })

    getLikes();


    return false; 
}


function addLike() {


    $.ajax({
        type: "POST",
        url: "likedata.php",
        data: { arg1: "<?php echo $_SESSION['userId']?>", arg2: "<?php echo $p;?>", arg3: "1" }
    })


    getLikes();


    return false; 
}



$(document).ready(function() {   getLikes();
 $(".UnLike").live('click',removeLike);
 $(".Like").live('click',addLike);


});



    </script>

likelist.php:

<?php
require $_SERVER['DOCUMENT_ROOT'].'/view.class.php';

$view = new view();
include $_SERVER['DOCUMENT_ROOT'].'/profile.class.php';
include $_SERVER['DOCUMENT_ROOT'].'/init.php';  

$profile = new profile($dbh);

if(isset($_POST)){
$p = $_POST['p'];


$view->printLikes($profile->getLikes($p));


}

likedata.php:

<?php
include $_SERVER['DOCUMENT_ROOT'].'/profile.class.php';
include $_SERVER['DOCUMENT_ROOT'].'/init.php';  

$profile = new profile($dbh);

if(isset($_POST)){
$liker = $_POST['arg1'];
$likee = $_POST['arg2'];
$likeYesNo = $_POST['arg3'];

$profile->insertLikes($liker, $likee, $likeYesNo);


}
?>

2 个答案:

答案 0 :(得分:2)

AJAX是非常的,因此getLikes函数将在addLikeremoveLike中完成AJAX之前触发。您肯定需要将getLikes放入$.ajax的成功回调中,以便它不会检索可能尚未更新的数据

function addLike() {
    $.ajax({
        type: "POST",
        url: "likedata.php",
        data: { arg1: "<?php echo $_SESSION['userId']?>", arg2: "<?php echo $p;?>", arg3: "1" },
        success: getLikes
    })

}

答案 1 :(得分:1)

好的......这是我从使用ajax重复调用中学到的......

IE非常讨厌他们,有时候他们只是按照他们应该的方式工作。

试试这个

function addLike() {

    var randnum = Math.floor(Math.random()*1001); //Add This Here on all Ajax Calls

    $.ajax({
        type: "POST",
        url: "likedata.php",
        cache: false, //Add This Here - Assists in helping Browsers not to cache the Ajax call
        data: yourdata + '&random=' + randnum, // Add this to the end of your data you are passing along *'&random=' + randnum,*
        success: function() {
              getLikes();
        }
    })

}

添加随机数据会导致浏览器认为它是一个新的呼叫。


此外, random = randnum 不会影响php端的任何内容。