CharlieAus
CharlieAus

Reputation: 83

Return false on jQuery post?

I'm passing a value for an insert and adding + 1 but I thought success: function(html) would not execute if my check returned that the value already existed (echo its already there in the PHP). Even if the insert does not happen, success gets executed, why is that ?

How do I get the AJAX to execute only if the insert happens (If the value doesn't yet exist)

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 ;
?>

Upvotes: 0

Views: 734

Answers (3)

user1968030
user1968030

Reputation:

Use this code

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

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Since you are returning success in case of a successfull insert you can check for that value in the success callback

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

Upvotes: 1

Adil Shaikh
Adil Shaikh

Reputation: 44740

You can do this (as you are echoing success on successful query) -

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

Upvotes: 1

Related Questions