我已閱讀了一些類似的問題,並試圖將其放到我的網站上工作,但它不起作用(當您單擊鏈接時沒有控制檯和數據庫上的響應未更新)。用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}' –