2014-09-21 207 views
0

有人可以告訴我爲什麼我的按鈕不工作?我在我的博客上附加了一個類似按鈕,並使用Ajax將每個相似的計數器發送到mySQL數據庫中。但是,顯然這不起作用。這裏是我的jQuery函數:jquery阿賈克斯按鈕

$(document).ready(fuction() { 


    function likefunction(catch_blog_id) { 

    var count = 1; 
    var catch_id = catch_blog_id; 
    var status = $('#likelink').html(); 
    var vardata = 'count' + count + '&catch_id' + catch_id; 


    if (status == "Like") { 
    $.ajax({ 

     type: "POST", 
     url: 'like.php', 
     data: vardata, 
     success: function() {$('#likelink').html("Unlike");} 

    }) 

    } else { 

    $.ajax({ 

     type: "POST", 
     url: 'unlike.php', 
     data: vardata, 
     success: function() {$('#likelink').html("Like");} 

    }) 
    } 
    } 
    }) 
</script> 

這裏是.php文件:一個用於類似功能和其他的不同功能==

like: 

<?php 

include_once('connectserver.php'); 

$catch_blog_id = $_POST['catch_id']; 
$catch_count = $_POST['count']; 


$query_blog = mysql_query("SELECT `likes` FROM `posts` WHERE `id` = '$catch_blog_id'"); 

while($get_rows = mysql_fetch_assoc($query_blog)) { 


$likes = $get_rows['likes']; 
$likes = (int)$likes; 
$likes++; 

mysql_query("UPDATE `posts` SET `likes` = '$likes' WHERE `id` = '$catch_blog_id'"); 

} 







    ?> 


    unlike: 



    <?php 

    include_once('connectserver.php'); 

    $catch_blog_id = $_POST['catch_id']; 
     $catch_count = $_POST['count']; 


     $query_blog = mysql_query("SELECT `likes` FROM `posts` WHERE `id` = '$catch_blog_id'"); 

    while($get_rows = mysql_fetch_assoc($query_blog)) { 


    $likes = $get_rows['likes']; 
    $likes = (int)$likes; 
    $likes--; 

     mysql_query("UPDATE `posts` SET `likes` = '$likes' WHERE `id` = '$catch_blog_id'"); 

     } 







    ?> 

這裏的一部分用於存儲類似按鈕的html文件:

echo「
贊評論」;

+0

您的javascript代碼中的計數總是等於1(在變量中'vardata')。此外,您需要在$ _POST數據中的變量和值之間有相同的符號。 – Sablefoste 2014-09-21 23:57:44

+0

計數是多餘的。我shud已刪除該部分...忽略計數..我將後端sql數據設置爲0,並通過從查詢中提出它來增加它 – 2014-09-22 00:05:58

回答

0

這就像在服務器端看不到vardata,爲什麼注意似乎不工作。嘗試使用

vardata = { 
      count: count, 
      catch_id: catch_id 
} 


$.post(like.php, vardata, function(res){ 
      $('#likelink').html("Unlike"); 
}); 
+0

什麼是回調,我該如何使用它?它與我使用的成功功能相似嗎? – 2014-09-22 01:19:48

+0

@JOSEPHALCATRAZ回調只是成功的功能,所以你可以使用它像功能(res){$('#likelink')。html(「Like」);} – codemonk 2014-09-22 07:25:02

+0

它不起作用... – 2014-09-22 10:39:42

0

使用此JavaScript代碼:

<script type="text/javascript"> 

    function likefunction(catch_blog_id) { 
     var count = 1; 
     var catch_id = catch_blog_id; 
     var status = $('#likelink').html(); 
     var vardata = 'count=' + count + '&catch_id=' + catch_id;  

     if (status == "Like") { 
      $.ajax({    
       type: "POST", 
       url: 'like.php', 
       data: vardata, 
       success: function() {$('#likelink').html("Unlike");}    
      }); 

     } else { 

      $.ajax({    
       type: "POST", 
       url: 'unlike.php', 
       data: vardata, 
       success: function() {$('#likelink').html("Like");}   
      }); 
     } 
    } 
</script> 

這PHP代碼like.php:

<?php 
include_once('connectserver.php'); 

$catch_blog_id = $_POST['catch_id']; 

mysql_query("UPDATE `posts` SET likes = likes + 1 WHERE `id` = '$catch_blog_id'"); 

?> 

而對於unlike.php這個PHP代碼:

<?php 
include_once('connectserver.php'); 

$catch_blog_id = $_POST['catch_id']; 

mysql_query("UPDATE `posts` SET likes = likes - 1 WHERE `id` = '$catch_blog_id'"); 

?> 
+0

在這工作在'posts'中'like'字段的數據類型應該是'INTEGER(或INT),SMALLINT,TINYINT,MEDIUMINT和BIGINT'。 – 2014-09-22 11:14:33