2015-05-25 38 views
-5

我想用jquery ajax發表評論,同時用jQuery ajax抓取發表的評論。任何人都可以幫助我與jQuery的Ajax?我想用jquery發表評論ajax

+0

你到目前爲止做了什麼?在這裏分享代碼 – Optimus

+1

搜索_jQuery ajax post tutorial_或者閱讀文檔 - 然後回來閱讀[help],然後詢問 – mplungjan

+0

以及我知道如何發佈評論到我的php代碼,但之後,我需要獲取評論張貼 –

回答

1

你的問題不是很清楚,你應該進一步解釋你需要什麼,並給出你到目前爲止的樣本代碼。

我希望這有助於

HTML代碼

<div id='old_comment'></div> 
<textarea id='comment'></textarea> 
<button id = 'btn'>Post Comment</button> 

jQuery代碼

$("document").ready(function() { 
    $("#btn").click(postComent); 

}); 

function postComent(){ 
    $("#old_comment").html("posting comment ..."); 
    $.ajax({ 
     url : 'url to php script', 
     data : { 
      comment : $("#comment").val() 
     }, 
     datatype : "json", 
     type : 'post', 
     success : function(result) { 
       $("#old_comment").html(result); 
     }, 
     error : function() { 
      alert("Error reaching the server. Check your connection"); 
     } 
    }); 
} 

PHP代碼

<?php 
mysql_connect("localhost", "root", "root") or die("connecting");        
mysql_select_db("my_db")or die("database"); 
$com = $_POST['comment']; 

$query = "INSERT INTO comments(comment) VALUES('".$com."')"; 
mysql_query($query); 

mysql_connect("localhost", "root", "root") or die("connecting")       
mysql_select_db("my_db")or die("database"); 
$sql = "SELECT comment FROM comment"; 
     $res = mysql_query($sql); 
     $result = ""; 
     while($row = $res->fetch_row()){ 
       $result .= $row[0]."<br/>"; 
     } 
     echo $result; 
?> 

數據庫結構

Create database my_db; 
Create table comments int id auto_increment, varchar(255) comment not null; 
+0

是的,現在很清楚......謝謝 –