我已阅读了一些类似的问题,并试图将其放到我的网站上工作,但它不起作用(当您单击链接时没有控制台和数据库上的响应未更新)。用html链接更新数据库点击使用ajax php mysql
以下是我想要做的:我希望用户通过单击评论旁边的图标来评论评论+1
。我想要更新我的mysql comment_table
列rating
与rating+1
。当我没有AJAX的时候(也就是将表单动作设置为php page?id=10
),它可以正常工作。我无法让AJAX更新数据库。
我与评论主页:
<a href="javascript:void(0);" onClick="updateRating(1,<?php echo $thisperspective_row['id']; ?>)" alt="UPVOTE" id="upvote_<?php echo $thisperspective_row['id']; ?>"><span class="glyphicon glyphicon-chevron-up"></span></a>
该链接下的javascript:
<script type="text/javascript">
function updateRating(rating, id){
$.ajax({
type: "GET",
url: "rating.php",
mode: "vote",
rating: rating,
id: <?php echo $thisperspective_row['id']; ?>,
success: function(response) {
console.log(response);
}
});
return false; // Prevent the browser from navigating to the-script.php
};
</script>
和我rating.php文件
<?php
require_once('connectiontodatabase.php');
/* simple comment up and down voting script */
$mode = $_GET['mode'];
$rating = $_GET['rating'];
$id = $_GET['id'];
if ($mode=="vote")
{
// The name of the
$cookie = "nameofmycookie".$id;
if(isset($_COOKIE[$cookie]))
{
echo '<div class="alert alert-warning"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> Sorry You have already rated this comment within the last 14 days.</div>';
}
else
{
$expiry = 1209600 + time(); // 14 day expiry
setcookie ($cookie, "voted", $expiry);
mysql_query ("UPDATE comment_table SET rating = rating+$rating WHERE id=$id", $connection);
}
}
?>
的PHP运行正常,并当我查看源代码时,所有变量都已正确列出。但是,当我单击链接时,根本没有响应,并且控制台不输出响应。我究竟做错了什么?提前致谢!
在阿贾克斯你需要发送的数据是这样,'数据:{模式:“投票”,等级:等级,编号:ID}' –