PHP ?? ??? ?? ?? ???? PHP ???
?? PHP? JSON ???? ?? ???? server.php ??? ???????.
??? ??? ????
<?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); ?>
??: json_encode ??? ????? PHP ??? 5.2 ????? ???.
comments.php ??
comment.php? ??? ??? ajax?? ??? ??? ? ?? ??? ????? ???? ????? ???? ??? ? ???? ??????? ?????. ?? 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 "評(píng)論內(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 ??? ???? ?? ??? ??? ? ????.
? ??? ???? ?? ??? ???? ??? ???? jQuery ??? ?????. ?? ??? ?? PHP? ajax ?? ????? ?? ????. jQuery? ?? ??? ??? ? ???? ??? ?? ? ????.