2

我正在尝试在页面上制作一个赞按钮,但似乎无法使其正常工作。基本上有三个函数使用 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));


}

喜欢data.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);


}
?>
4

2 回答 2

2

AJAX 是 ayshcronous,因此这些getLikes函数将在 AJAX 完成之前addLike触发removeLike。您肯定需要放入getLikes成功回调中,$.ajax这样它就不会检索可能尚未更新的数据

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

}
于 2012-11-02T00:49:49.113 回答
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 产生任何影响。

于 2012-11-03T00:06:01.017 回答