我有处理喜欢的sendlike.php文件,到目前为止可以使用!
<?php
//include db configuration file
include_once("config.php");
//Include functions
include_once("functions.php");
// http://stackoverflow.com/questions/21065502/ajax-a-href-onclick-get-php-variable-and-execute-php-file
// 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);
//Record was successfully inserted, respond result back to index page
// This should probably in the fute go thruh functions.php comment_func_bar
$my_id = $mysqli->insert_id; //Get ID of last inserted row from MySQL. Will this cause problems when alot of ppl liking / posting / commenting etc??
echo '<a href="sendlike.php?delike='.$comment_id_fk.'" class="clickable">' . $row_array['TOTAL_COMMENT_LIKES'] .' Unlike</a>';
$mysqli->close(); //close db connection
}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
}
else
{
//Output error
header('HTTP/1.1 500 Error occurred, Could not process request!');
exit();
}
?>
如果您查看回显行,则href的类为class =“ clickable”。和sandlike.php已更改为不喜欢。
我无法执行AJAX脚本,该脚本将发送
<a href="sendlike.php?like=' . $comment_id .'" class="clickable">
并将“ clickable” href更新为<a href="sendlike.php?delike='.$comment_id_fk.'" class="clickable">' . $row_array['TOTAL_COMMENT_LIKES'] .' Unlike</a>'
就我来了
<script type="text/javascript">
$('.clickable').on('click', function() {
var data = {
mode: "like",
rating: $(this).data('rating'),
id: $(this).data('id')
};
$.ajax({
type: 'GET',
url: 'sendlike.php',
data: data,
success: function(response) {
console.log(response);
}
});
});
</script>
而且甚至不上班。
还有class =“ clickable”,我在页面上有多象素注释。都会得到喜欢/不喜欢。还是我需要class =“ item_ $ number_clickable”之类的东西?
提前致谢!
最佳答案
添加event.preventDefault()
并将事件委托给静态父对象:
$(document).on('click', '.clickable', function(e) {
e.preventDefault(); // to stop the page navigation