2013-03-03 48 views
0

我正在研究一個基於php的小型網站,我希望用戶能夠評論其他用戶的帖子。我使用jquery文章將用戶評論發送給一個將其添加到數據庫的php腳本,但是我發現儘管jquery發佈警告「成功」,但評論偶爾不會添加到數據庫中。我的代碼如下:在textarea註釋字段上發佈不可靠的JQuery帖子

jQuery的崗位(職位的ID分配給textarea的)

$('.post_comment').live('click', function(){ 
    entry = $(this).parent(); 
    var textarea = entry.find('.commentbox'); 
    textarea_id = textarea.attr('id'); 
    textarea_value = textarea.val(); 
    if(textarea_value.length > 0) 
    { 
     $.post('add_comment.php', { id : textarea_id, value : textarea_value } , function(data){ 
      alert("Success"); 
     }); 
    } 

add_comment.php

<?PHP 
session_start(); 
$currentUser = $_SESSION['id']; 
include("connect.php"); 
$id = $_POST['id']; 
$value = $_POST['value']; 
mysql_query("INSERT INTO comment (post,user,content,date) VALUES ('$id','$currentUser','$value',NOW())"); 
echo mysql_insert_id(); 
mysql_close($connection); 
?> 

我不知道這是否是我的代碼,或者如果它只是一個不可靠的網絡主機。任何幫助將非常感激。

+0

你並沒有逃避你的價值 - 這是第一步。 – 2013-03-03 10:07:05

+0

在add_comment.php中使用mysql_error()檢查錯誤 – 2013-03-03 10:10:21

+1

這與您的問題無關,但您應該使用.on而不是.live,因爲.live不再受支持http://api.jquery.com/live/您可以use.on以相同的方式使用.live – Peeyush 2013-03-03 10:34:31

回答

1

textarea_value = encodeURIComponent(textarea.val());