anonymous No title
No License PHP
2021年05月26日
Copy Clone
<!DOCTYPE html>
<html lang="ja" >
  <head>
    <meta charset="utf-8">
    <title>簡易掲示板</title>
  </head>

  <body>
    <h1>簡易掲示板</h1>
    <form action="" method="post">
      
      <p>名前</p>
      <input type="text" name="user_name"><br>
      
      <p>コメント</p>
      <textarea name="comment" rows="4" cols="40"></textarea><br>
     
      <input type="submit" value="送信">
    </form>
<?php

error_reporting(0);
  
    $name=$_POST["user_name"];
   
   $comment=$_POST["comment"];
   
   $date=date("Y-m-d H:i:s");
  
   $filename="kadai_2_3(3).txt";
   
   $num=count(file($filename))+1;
   
   $parts="$num<>$name<>$comment<>$date";
  
   $fp=fopen($filename,"a");

    if(empty($name) && empty($comment)){
       echo "未記入箇所があります。<br>";
   }
  
   elseif(empty($name) ){
       echo "名前を入力してください<br>";
   }
  
   elseif(empty($comment)){
       echo "コメントを入力してください<br>";
   }
   
   elseif(isset($name) && isset($comment)){
     fwrite($fp,"$parts".PHP_EOL);
     fclose($fp);
   }
  
   if(file_exists($filename)){
     
     $lines=file($filename,FILE_IGNORE_NEW_LINES);
     foreach($lines as $line){
      
       list($num,$name,$comment,$date)=explode("<>",$line);
      
       echo "$num<>$name<>$comment<>$date"."<br>";
     }
   }
?>
  </body>
</html>
<!DOCTYPE html>
<html lang="ja" >
  <head>
    <meta charset="utf-8">
    <title>簡易掲示板</title>
  </head>

  <body>
    <h1>簡易掲示板</h1>
    <form action="" method="post">
      
      <p>名前</p>
      <input type="text" name="user_name"><br>
      
      <p>コメント</p>
      <textarea name="comment" rows="4" cols="40"></textarea><br>
     
      <input type="submit" value="送信">
    </form>
<?php

error_reporting(0);
  
    $name=$_POST["user_name"];
   
   $comment=$_POST["comment"];
   
   $date=date("Y-m-d H:i:s");
  
   $filename="kadai_2_3(3).txt";
   
   $num=count(file($filename))+1;
   
   $parts="$num<>$name<>$comment<>$date";
  
   $fp=fopen($filename,"a");

    if(empty($name) && empty($comment)){
       echo "未記入箇所があります。<br>";
   }
  
   elseif(empty($name) ){
       echo "名前を入力してください<br>";
   }
  
   elseif(empty($comment)){
       echo "コメントを入力してください<br>";
   }
   
   elseif(isset($name) && isset($comment)){
     fwrite($fp,"$parts".PHP_EOL);
     fclose($fp);
   }
  
   if(file_exists($filename)){
     
     $lines=file($filename,FILE_IGNORE_NEW_LINES);
     foreach($lines as $line){
      
       list($num,$name,$comment,$date)=explode("<>",$line);
      
       echo "$num<>$name<>$comment<>$date"."<br>";
     }
   }
?>
  </body>
</html>
No one still commented. Please first comment.
Output