Okey,我有一个functions.php,可以处理一些函数。
一个是CCD_ 1。
此函数在每个帖子评论的底部输出No likes · Like · Report
。
我有一个处理mysql数据的sendlike.php。
<?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,但没有成功。
将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 的建议