2015-10-27 85 views
0

我試圖將表中使用jquery的變量thecode轉換爲名爲getComments()的函數。我的代碼如下。首先,我有我的jQuery腳本是這樣的:將表中的值傳遞給使用jquery的函數

$(document).ready(function(){       
    $("#comment_process").click(function(){ 
     if($("#comment_text").val() != ""){ 
     $('.post_loader').show(); 
      $.post("comments_business.php?action=post", { 
       comment: $("#comment_text").val() 
      }, function(data) { 
       $(".comments").hide().html(data).fadeIn('slow'); 
       $("#comment_text").val(""); 
       $('.post_loader').hide(); 
      }); 
     } 
    }); 
}); 

接下來我有HTML和PHP下面的腳本:

<!--- more code at the top----> 
<?php $auto = $profile_data_business['business_code']; ?> 
<table>        
    <textarea rows="3" id="comment_text" placeholder="share an update."></textarea> 
    <input type="" id="comment_code" name="thecode" value="<?php echo $auto; ?>" />        
    <input type="button" id="comment_process" /> 
</table> 
<div class="comments"><?php include_once("comments_business.php");?> </div> 

命名爲comments_business.php該頁面包含一個函數如下:

<?php 
function getComments(){  
    $session_user_id = $_SESSION['user_id']; 
    $comments = ""; 
    // can't get variable $thisemail 
    $thisemail = mysql_real_escape_string($_POST['thecode']); 
    $sql = mysql_query("SELECT * FROM comments_business WHERE (`flag`=0 and `user`='$thisemail' and `comments_id` NOT IN (SELECT `comments_id` FROM `hide_comment_business` where `user`='$session_user_id')) ORDER BY comment_date DESC LIMIT 40") or die (mysql_error()); 
    //more code here    
    return $comments;  
} 
?> 

任何想法如何更改我的jQuery代碼,以便我將能夠將$thisemail變量成功地通過getComments() func重刑?

+0

問題是,您如何/在哪裏調用'getComments()'函數並啓動會話? –

+0

是會話已經開始 – 33528

+0

你也沒有'

回答

1
  • 當您使用$.post時,不需要在URL(action = post)中寫入GET參數。
  • 當您發表的數據根據​​評論的名字,您必須通過某個名稱的 獲取數據php$_POST['comment'])。
  • 當您使用ajax時,不應在php中使用function或在定義後調用function
  • 當您使用ajax必須printecho數據php文件顯示在 後的結果。