我傳入一個值插入並添加+ 1,但我認爲成功:函數(HTML)不會執行,如果我的檢查返回該值已經存在(回聲它已經存在在PHP中)。即使插入沒有發生,成功也會被執行,爲什麼呢?在jQuery post上返回false?
如何讓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 ;
?>
應該如何JavaScript的知道,如果插入了檢查該值成功與否?它不懂英文。 – JJJ
這是不幸的......但是我真的很清楚你的意思,我在寫我的問題時就明白了 – CharlieAus