PHP development...LOGIN

PHP development post comment function tutorial PHP page

Let’s first look at the server.php code for PHP to read and generate JSON data.

0.jpg

code show as below

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$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);
?>

Note: Your PHP version should be 5.2 or above to use the json_encode function.


comments.php code

##comment.php receives the nickname and comment content parameters submitted by the front desk ajax, and determines The parameters are legal, and then the data is inserted into the database. If successful, 1 is output and returned to the front-end jQuery for processing.

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$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 "评论内容不能为空!";
   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";
?>


We can implement our comment function by combining our HTML page with PHP code


This example uses simple and easy code to explain the lightweight and efficient jQuery combined with PHP's ajax operating mechanism. Of course, this is just a basic example. jQuery can also do many things, which is left to everyone. Go ahead and have fun.


<?php header("Content-type:text/html;charset=utf-8"); //设置编码 $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); ?>
submitReset Code
ChapterCourseware