Php 自动刷新ajax隐藏类bot not id
好的,我有一个functions.php来处理一些函数 一个是Php 自动刷新ajax隐藏类bot not id,php,jquery,ajax,Php,Jquery,Ajax,好的,我有一个functions.php来处理一些函数 一个是函数注释功能条 此函数在每个帖子评论的底部输出No likes·Like·Report 我有一个sendlike.php来处理mysql数据 <?php //include db configuration file include_once("config.php"); //Include functions include_once("functions.php"); // For practice $u
函数注释功能条
此函数在每个帖子评论的底部输出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();
}
?>
插入到数据库可以工作,但不能自动刷新。它还使用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>
$(文档).on('click','clickable',函数(e){
e、 预防默认值();
var action=$(this.html();
var comment_id=$(this.attr('id');
如果(动作=='Like')
{
var data={like:comment_id};
var status=“不像”;
}
其他的
{
var data={delike:comment_id};
var status=“Like”;
}
$.ajax({
键入:“获取”,
url:“sendlike.php”,
数据:数据,
数据类型:“文本”,
成功:功能(响应){
$(“.txtHint_uu”+注释_id).hide();
$(“.txtHint_uu”+comment_id).html(“”);
$(“.txtHint_u”+注释_id).append(response.fadeIn();
},
错误:函数(xhr、ajaxOptions、thrownError){
//出现错误时,我们会提醒用户
警报(thrownError);
}
});
});
并从@EhsanT获得关于sendlike.php的建议首先,您必须隐藏并淡出相应id的注释,而不是使用
可点击的类的所有注释。其次,您可以在sendlike.php
文件中生成有关注释(喜欢或不喜欢)状态的HTML
代码,并将其回显。然后在axaj
中分析响应,并在成功响应后显示正确的链接。
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>';
}
<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>