Okey,我有一个处理某些功能的functions.php。
一个是function comment_func_bar
。
此功能会在每条评论的底部输出No likes · Like · Report
帖子。
我有sendlike.php来处理mysql数据。
<?php
//include db configuration file
include_once("config.php");
//Include functions
include_once("functions.php");
// For practice
$uid_fk = 1;
//check $_POST["content_txt"] is not empty
if(isset($_GET["like"]) && strlen($_GET["like"])>0)
{
//Add IP, date. Set the Foreign key
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("Y-m-d H:i:s");
$comment_id_fk = $_GET["like"]; // '".$comment_id_fk."', What comment has been liked?
// $_SESSION["user_id"] '".$uid_fk."', What user has liked it? Get the users uid
// Insert sanitize string in record
$insert_row = $mysqli->query("INSERT INTO comment_likes (comment_id_fk, uid_fk,date,ip)
VALUES('".$comment_id_fk."','".$uid_fk."','".$date."','".$ip."')");
if($insert_row)
{
//Count the amount of likes again
$count_likes=$mysqli->query("SELECT COUNT(*) as TOTAL_COMMENT_LIKES FROM `comment_likes`
WHERE comment_id_fk='".$comment_id_fk."'");
$row_array=$count_likes->fetch_array(MYSQLI_ASSOC);
$mysqli->close(); //close db connection
// header('Location: ' . $_SERVER['HTTP_REFERER']); //ADD
}else{
//header('HTTP/1.1 500 '.mysql_error()); //display sql errors.. must not output sql errors in live mode.
header('HTTP/1.1 500 Looks like mysql error, could not insert record!');
exit();
}
}
elseif(isset($_GET["delike"]) && strlen($_GET["delike"])>0 && is_numeric($_GET["delike"]))
{ //do we have a delete request? $_POST["recordToDelete"]
//sanitize post value, PHP filter FILTER_SANITIZE_NUMBER_INT removes all characters except digits, plus and minus sign.
$idToDelete = filter_var($_GET["delike"],FILTER_SANITIZE_NUMBER_INT);
//try deleting record using the record ID we received from POST
$delete_row = $mysqli->query("DELETE FROM comment_likes WHERE comment_id_fk='".$idToDelete."' AND uid_fk ='".$uid_fk."'"); //uid_fk is $_SESSION[user_id] actually
if(!$delete_row)
{
//If mysql delete query was unsuccessful, output error
header('HTTP/1.1 500 Could not delete record!');
exit();
}
$mysqli->close(); //close db connection
// header('Location: ' . $_SERVER['HTTP_REFERER']); //ADD
}
else
{
//Output error
header('HTTP/1.1 500 Error occurred, Could not process request!');
exit();
}
?>
我想用AJAX
做这件事<script type="text/javascript">
$(document).on('click', '.clickable', function(e) {
e.preventDefault();
var action = $(this).html();
// var comment_id = this.id;
var comment_id = $(this).attr('id');
if(action == 'Like')
{
var data ={like:comment_id};
}
else
{
var data ={delike:comment_id};
}
$.ajax({
type: "GET",
url: "sendlike.php",
data:data,
dataType: "text",
success: function(response){
$(".clickable").hide();
$(".clickable").append(response);
$(".clickable").fadeIn();
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
});
</script>
点击赞(或不喜欢)之后,喜欢的数量应自动刷新,喜欢应改为不同。
当我不使用Ajax时,comment_fun_bar
会处理此问题。
echo '<p id="txtHint"><small>' . $how_many .' <a href="#" class="clickable" id="'.$comment_id .'">Like</a> · <a href="">Report</a>'.$owner.'</small></p>';
}
else
{
echo '<p id="txtHint"><small>' . $how_many .' <a href="#" class="clickable" id="'.$comment_id .'">Unlike</a> · <a href="">Report</a>'.$owner.'</small></p>';
}
插入数据库有效,但不能自动刷新。它还隐藏和淡化所有注释与class =“clickable”。有人请求帮助我。
似乎我还需要在Ajax函数中隐藏href id。但是不知道怎么做。试图使用#comment_id,但没有运气。
答案 0 :(得分:0)
将ajax更改为:
<script type="text/javascript">
$(document).on('click', '.clickable', function(e) {
e.preventDefault();
var action = $(this).html();
var comment_id = $(this).attr('id');
if(action == 'Like')
{
var data ={like:comment_id};
var status="Unlike";
}
else
{
var data ={delike:comment_id};
var status = "Like";
}
$.ajax({
type: "GET",
url: "sendlike.php",
data:data,
dataType: "text",
success: function(response){
$(".txtHint_"+comment_id).hide();
$(".txtHint_"+comment_id).html("");
$(".txtHint_"+comment_id).append(response).fadeIn();
},
error:function (xhr, ajaxOptions, thrownError){
//On error, we alert user
alert(thrownError);
}
});
});
</script>
并接受@EhsanT关于sendlike.php
的建议