【发布时间】:2014-05-20 13:11:46
【问题描述】:
我已经阅读了许多类似的问题,并尝试在我的网站上使用它,但它不起作用(当您单击链接时,控制台上没有响应并且数据库没有更新)。
这就是我想要做的:我希望用户通过点击评论旁边的图标来评价评论+1。我希望它用rating+1 更新我的名为rating 的mysql comment_table 列。当我在没有 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 运行良好,所有变量都正确列出。但是,当我单击链接时,根本没有响应,控制台也没有输出响应。我究竟做错了什么?提前致谢!
【问题讨论】:
-
在 Ajax 中你需要像这样发送数据,
data: {mode: "vote", rating: rating, id: id}
标签: javascript php jquery mysql ajax