2014-04-08 228 views
1

我已閱讀了一些類似的問題,並試圖將其放到我的網站上工作,但它不起作用(當您單擊鏈接時沒有控制檯和數據庫上的響應未更新)。用html鏈接更新數據庫點擊使用ajax php mysql

以下是我想要做的:我希望用戶通過單擊評論旁邊的圖標來評論評論+1。我想要更新我的mysql comment_tableratingrating+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運行正常,並當我查看源代碼時,所有變量都已正確列出。但是,當我單擊鏈接時,根本沒有響應,並且控制檯不輸出響應。我究竟做錯了什麼?提前致謝!

+0

在阿賈克斯你需要發送的數據是這樣,'數據:{模式:「投票」,等級:等級,編號:ID}' –

回答

2

首先你應該改變的方式你正在檢測點擊事件。退房this fiddle。然後,您需要使用數據選項在一個JSON string中傳遞所有變量。你的代碼應該是這個樣子:

<span class="glyphicon glyphicon-chevron-up clickable" 
    data-rating="1" 
    data-id="<?php echo $thisperspective_row['id']; ?>"></span> 

<script type="text/javascript"> 
    $('.clickable').on('click', function() { 
     var data = { 
      mode: "vote", 
      rating: $(this).data('rating'), 
      id: $(this).data('id') 
     }; 
     $.ajax({ 
      type: 'GET', 
      url: 'rating.php', 
      data: data, 
      success: function(response) { 
       console.log(response); 
      } 
     }); 
    }); 
</script> 
+0

謝謝@Styphon。當我對你的建議代碼進行了一個改變時,你的解決方案就工作了: 而不是輸入'$ .ajax(function(){'我輸入'.ajax({' – GK79

+0

@ GK79是的,對於大多數jQuery,在那裏,但不是爲了ajax,我必須在習慣中加入它,並且更新我的答案,很高興幫助。 – Styphon

0

首先關閉所有,請檢查您是否加載了jQuery,然後用這個代碼

function updateRating(rating, id){ 
    $.ajax({ 
    type: "GET", 
    url: "rating.php", 
    mode: "vote", 
    data: { rating: rating, id: id }, 
    success: function(response) { 
    console.log(response); 
    } 
    }); 
return false; // Prevent the browser from navigating to the-script.php 
}; 

爲我工作。 注意,我刪除了`裏面阿賈克斯,因爲你已經在函數發送參數,可以同時發送PARAMS你必須使用數據:,你可以在這裏看到更多的例子Ajax jQuery

+0

感謝您的答覆。我沒有足夠先進的jQuery來理解爲什麼鏈接的onClick事件不能觸發該函數(檢出@ Styphon的小提琴)。它不適合我,所以我決定採用Styphon的上述解決方案。感謝您的回覆! – GK79

+0

謝謝,我還建議你將'mysql_real_escape_string()'添加到你從用戶那裏獲得的任何** GET **或** POST **中。 你可以在這裏看到更多關於該功能的信息。 [鏈接](http://php.net/mysql_real_escape_string) – robzdc