2012-03-26 39 views
0

所以即時嘗試讓jquery發送ajax調用來更新評論數據庫。我似乎無法找到代碼有什麼問題。一直在互聯網上試圖找出答案。使用jQuery發送ajax調用

<div class="comment"> 
<textarea name="comment" onkeydown="test(event)"></textarea> 
</textarea> 
</div> 

    function test(event) { 
    if (event.keyCode==13) { 
     var comment = $('textarea[name=comment]'); 
     $.ajax({ 
      type: "GET", 
      url: "commentupdate.php", 
      data: "comment=" + comment, 
     }); 
     $('.comment').hide(); 
     $('#container').isotope('reLayout'); 
    } 
} 

commentupdate.php

include('MasterHub_DBCon.php'); 
$COOKIEINFO = $_COOKIE['masterhub_user']; $USERCOOKIEINFO = explode('+', $COOKIEINFO); 
$comment = $_REQUEST['comment']; 
$sql = "INSERT INTO `dmech_main`.`COMMENTS` (`ID`, `USERNAME`, `COMMENT`, `COMMENTID`) VALUES (NULL, \'' .    $COOKIEINFO['0'] . '\', \'' . $comment . '\', \'' . $_SESSION['COMMENTID'] . '\');"; 
mysql_query($sql); 
+0

所以什麼錯誤? – 2012-03-26 22:24:33

+0

你可以發佈commentupdate.php的代碼,所以我們知道它期待什麼? – 2012-03-26 22:24:58

+0

so是commentupdate.php命中?或者打沒有評論?要麼 ...? – 2012-03-26 22:25:58

回答

0

我會嘗試把javascript中的

<script> 

標籤開始的內部。

+0

當然,我做到了這一點,我試圖給儘可能小的信息儘可能 – 2012-03-26 22:28:22

0

要麼你需要自己的數據進行編碼,並通過一個串,或通過對象:

1:

<script type="text/javascript> 

function test(event) { 
    if (event.keyCode==13) { 
     var comment = $('textarea[name=comment]'); 
     $.ajax({ 
     type: "GET", 
     url: "commentupdate.php", 
     data: "comment=" + encodeURIComponent(comment) 
     }); 
     $('.comment').hide(); 
     $('#container').isotope('reLayout'); 
    } 
} 

</script> 

2:

<script type="text/javascript> 

function test(event) { 
    if (event.keyCode==13) { 
     var comment = $('textarea[name=comment]'); 
     $.ajax({ 
     type: "GET", 
     url: "commentupdate.php", 
     data: { comment: comment } 
     }); 
     $('.comment').hide(); 
     $('#container').isotope('reLayout'); 
    } 
} 

</script> 

引用從http://api.jquery.com/jQuery.ajax/

數據

要發送到服務器的數據。它被轉換成查詢字符串,如果還不是字符串的話。它附加到GET請求的url。請參閱processData選項以防止此自動處理。對象必須是鍵/值對。如果value是一個數組,則jQuery會根據傳統設置的值(如下所述)使用相同的鍵序列化多個值。

+0

當我使用數據:{comment :評論}它完全凍結我的屏幕,直到我退出標籤 – 2012-03-26 22:40:38

+0

當這種情況發生時,你是否得到任何錯誤? (檢查JavaScript控制檯) – poncha 2012-03-26 22:41:54

+0

順便說一句,如果它的GET請求無論如何,那麼你也可以自己構建url ...「commentupdate.php?comment =」+ encodeURIComponent(comment) – poncha 2012-03-26 22:43:45

1

你錯過了val()我想。嘗試 -

var comment = $('textarea[name=comment]').val(); 

相反的 -

var comment = $('textarea[name=comment]');