PHP 개발 포스트 댓글 기능 튜토리얼 PHP 페이지

먼저 PHP가 JSON 데이터를 읽고 생성하는 server.php 코드를 살펴보겠습니다.

0.jpg

코드는 다음과 같습니다

<?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);
?>

참고: json_encode 기능을 사용하려면 PHP 버전이 5.2 이상이어야 합니다.


comments.php 코드

comment.php는 프런트 데스크 ajax에서 제출한 닉네임 및 댓글 콘텐츠 매개변수를 수신하고 매개변수의 적법성을 확인한 후 데이터를 데이터베이스에 삽입합니다. 출력 1, 처리를 위해 프런트엔드 jQuery로 돌아갑니다.

<?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";
?>


HTML 페이지와 PHP 코드를 결합하여 주석 기능을 구현할 수 있습니다.


이 예제는 간단하고 쉬운 코드를 사용하여 가볍고 효율적인 jQuery 조합을 설명합니다. 물론 이것은 단지 PHP의 ajax 작업 메커니즘의 기본 예입니다. jQuery는 많은 작업을 수행할 수 있으므로 누구나 즐길 수 있습니다.


지속적인 학습
||
<?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); ?>
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!