在jQuery帖子上返回false?

时间:2013-05-22 09:28:19

标签: jquery ajax

我正在为插入传递一个值并添加+ 1,但我认为成功:如果我的检查返回该值已经存在,则函数(html)将不会执行(在PHP中已经回显它)。即使插入没有发生,成功也会被执行,为什么会这样?

如果插入发生(如果该值尚不存在),我如何才能执行AJAX

JQUERY AJAX

$(function addThumbs() {
    $('.vote-up').bind('click', function() {
        // 'this' is a DOM element
        // wrap 'this' in a jQuery object
        var img = $(this);
        // now we have the full jQuery API at our disposal
        var user_id = $('#user_id').val();
        var thumbsElem = img.closest(".thumbs");
        var review_id = thumbsElem.find("#review_id").val();
        var score = thumbsElem.find("#pluses").html();
        var scoreupdated = parseInt(score) + 1;
        if (review_id == 1) 
        { alert('...'); }
        else
        {
            $("#flash").show();
            $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
            $.ajax({
                type: "POST",
                url: "ajax-thumbsup.php",
                data:{  
                    "user_id" : user_id, 
                    "review_id" : review_id       //we are passing the name value in URL
                },
                cache: false,
                success: function(html)
                {
                    img.attr('src', 'img/upgreen.png');
                    thumbsElem.find("#pluses").html(scoreupdated);
                }
            });
        }
        return false;
    });
});

PHP

<?php
$user_id = $_REQUEST['user_id'];
$review_id = $_REQUEST['review_id'];

require('inc/db.php');
$plusesfind = "SELECT * FROM reviews_likes WHERE review_id = :review_id AND user_id = :user_id";
$plusesfind = $conn->prepare($plusesfind);
$plusesfind->execute(array(':review_id' => $review_id, ':user_id' => $user_id));
if(!$pluses = $plusesfind->fetch()) :
    $con = mysql_connect("localhost","root","");

    mysql_select_db("mytvbox", $con);
    $sql = 'INSERT INTO reviews_likes (review_id, user_id, plus, minus) VALUES (' . $review_id .', ' . $user_id .', ' . 1 .' , ' . 0 .')';

    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    else{
        echo "success";
    }

    mysql_close($con);
else :

    echo "it's already there";
endif ;
?>

3 个答案:

答案 0 :(得分:1)

您可以执行此操作(,因为您在成功查询时回显success ) -

success: function(html){
    if(html === "success"){
       img.attr('src', 'img/upgreen.png');
       thumbsElem.find("#pluses").html(scoreupdated);
    }
}

答案 1 :(得分:1)

由于您在成功插入的情况下返回success,您可以在成功回调中检查该值

$.ajax({
    type: "POST",
    url: "ajax-thumbsup.php",
    data:{  
        "user_id" : user_id, 
        "review_id" : review_id       //we are passing the name value in URL
    },
    cache: false,
    success: function(html)
    {
        if(html == 'success'){
            img.attr('src', 'img/upgreen.png');
            thumbsElem.find("#pluses").html(scoreupdated);
        }
    }
});

答案 2 :(得分:0)

使用此代码

success: function(result){
    if(result=== "success"){
       img.attr('src', 'img/upgreen.png');
       thumbsElem.find("#pluses").html(scoreupdated);
    }
}