亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP開發(fā) 發(fā)表評論功能教程之PHP頁面

先來看PHP讀取和生成JSON數(shù)據(jù)的server.php代碼。

0.jpg

代碼如下

<?php
header("Content-type:text/html;charset=utf-8");    //設(shè)置編碼
$conn=mysqli_connect("localhost","root","root","comments");
mysqli_set_charset($conn,"utf8");
$sql="SELECT * from comments";
$que=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($que)){
      $comments[] = array("id"=>$row[id],"user"=>$row[user],"comment"=>$row[comment],"addtime"=>$row[addtime]);
}
echo json_encode($comments);
?>

注意:你的PHP版本應(yīng)該是5.2以上才能使用json_encode函數(shù)。


comments.php 代碼

comment.php接收前臺ajax提交過來的昵稱和評論內(nèi)容參數(shù),判斷參數(shù)的合法性,然后將數(shù)據(jù)插入到數(shù)據(jù)庫中,如果成功,則輸出1,返回給前臺jQuery處理。

<?php
header("Content-type:text/html;charset=utf-8");    //設(shè)置編碼
$user = htmlspecialchars(trim($_POST['user']));
$txt = htmlspecialchars(trim($_POST['txt']));
$time = date("Y-m-d H:i:s");
if(empty($user)){
   echo "昵稱不能為空!";
   exit;
}
if(empty($txt)){
   echo "評論內(nèi)容不能為空!";
   exit;
}
$conn=mysqli_connect("localhost","root","root","comments");
mysqli_set_charset($conn,"utf8");
$sql="insert into comments(user,comment,addtime)values('$user','$txt','$time')";
$que=mysqli_query($conn,$sql);
if($que)  echo "1";
?>


我們將我們的HTML頁面跟PHP代碼組合起來,就能實(shí)現(xiàn)我們的評論功能了


本例使用簡單容易的代碼詮釋了輕量、高效的jQuery結(jié)合PHP的ajax運(yùn)作機(jī)制,當(dāng)然這只是一個(gè)基礎(chǔ)的例子,jQuery還能做很多事情,留給大家去盡情發(fā)揮吧。


Weiter lernen
||
<?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $conn=mysqli_connect("localhost","root","root","comments"); mysqli_set_charset($conn,"utf8"); $sql="SELECT* from comments"; $que=mysqli_query($conn,$sql); while($row=mysqli_fetch_array($que)){ $comments[] = array("id"=>$row[id],"user"=>$row[user],"comment"=>$row[comment],"addtime"=>$row[addtime]); } echo json_encode($comments); ?>
einreichenCode zurücksetzen