插入文件

时间:2017-12-28 10:17:38

标签: php html css file

当我点击" Comment"时,它没有打开新文件$ cname.txt,因此无法在文件中插入值,因此没有发布任何内容。我希望在用户点击提交时发布cname和评论?这是我的代码:

 <?php
 $name="";
 /*print_r($_POST);*/
 if(isset($_POST['submit'])) { 
      global $name;
      $name=$_POST['name']; 
      $query=$_POST['query'];
      $my_query="My query";
     /* $files=fopen("$name.txt","w");
      fclose($files); */
      $po="$name <br> $my_query:$query \n";
      $myfile=fopen("posts.txt","a+");
      fwrite($myfile,$po);
      fclose($myfile);
 }
      $mypost=fopen("posts.txt","r");
        while($line = fgets($mypost)) {
        echo '<form class="w3-container w3-card-4 w3-light-grey w3-text-blue w3-margin">
        <div class="w3-row w3-section">
        <div class="w3-col" style="width:50px"><i class="fa fa-question-circle-o" style="font-size:30px"></i></div>
        <div class="w3-rest">
        <p class="w3-input w3-border"><i class="fa fa-user-circle-o"></i>'.$line; echo '</p>
    </div>
</div>
    <p>comments</p>
    <form method="post" action="' .htmlspecialchars($_SERVER['PHP_SELF']); echo '">
    Name:<textarea name="cname" rows="1" cols="4"></textarea><textarea name="comment" rows="1" cols="60"></textarea><br>
    <input type="submit" name="submits" value="Comment" class="w3-button w3-section w3-blue w3-ripple">
    </form>
    ' ; }
    $cname="";
    if(isset($_POST['submits'])) {
          global $cname;
          $cname=$_POST['cname']; 
          $comment=$_POST['comment'];
          $pos="$cname <br> $comment \n";
          $myfiles=fopen("$cname.txt","a+");
          fwrite($myfiles,$pos);
          fclose($myfiles);
          $myposts=fopen("$cname.txt","r");
          while($lines = fgets($myposts)) {
              echo $lines;
              }

    }'
    </form>'
    ;
      ?>

1 个答案:

答案 0 :(得分:0)

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">