douchigu1723 2014-07-10 03:06
浏览 26
已采纳

如何写入与HTML表单字段同名的文件

I have made an attempt to write a PHP code that takes the contents of an HTML form then write them into a file. I have that down just fine, but I have another problem. I want to take an input of a field in the form and make it the file name that I am writing to.

Here is my PHP code:

<?php
if(isset($_POST['forwhom']) && isset($_POST['importance']) && isset($_POST['message'])) {
$file = "students.html";
$data = nl2br('-' . $_POST['forwhom'] . ':' . ' ' . $_POST['message']  . ' ' . $_POST['importance'] . "
");
$ret = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
    die('There was an error writing this file');
}
else {
    echo "Success!  Student added to database.";
}
}
else {
   die('no post data to process');
}

?>

Currently, I am writing to the file "students.html" However, I want to write to the file "Dad.html", which happens to be the input in the field by the name of forwhom.

Basically, I am asking this: What do I use to replace "students.html" (line 3) so that the file name will be the same as the input of the field forwhom?

I apologize if that didn't make any sense. Thank you very much!

  • 写回答

1条回答 默认 最新

  • drzrzzkh462254 2014-07-10 07:55
    关注
    1. First check if data has been posted on your form using $_SERVER['REQUEST_METHOD'] == "POST".
    2. For simplicity sake save all your "POST" requests in a variable eg.

      $file = $_POST['forwhom'];
      $importance = $_POST['importance'];
      $message = $_POST['message'];

    3. I sometimes find it much easier using empty() instead of isset().

    4. Create a variable, lets say $status which would store all your messages, then any where in your HTML section you just use the $status to display the appropriate message to the user. Check below how i make use of $status in the form. By doing so is much cleaner and makes your code more dynamic in a sense.

               <?php
      
                  if ($_SERVER['REQUEST_METHOD'] == "POST") {
      
                      $file = $_POST['forwhom'];  
                      $importance = $_POST['importance'];
                      $message = $_POST['message'];
      
                      if (!empty($file) && !empty($importance) && !empty($message)) {
      
                          $data = nl2br('-' . $file . ':' . ' ' . $message  . ' ' . $importance . "
      ");      
                          $file .= ".html";       
                          $ret = file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
      
                          if ($ret == true) {
                              $status = "Success!  Student added to database.";
                          } else {
                              $status = "Error writing to file!";
                          }
                      } else {
                          $status = "Please enter name, email and message";
                      }
      
                  }
                  ?>
          <!DOCTYPE html>
          <html>
          <head>
              <title></title>
          </head>
          <body>
          <form action="" method="post">
              <ul>
                  <li>
                      <label for="name">Name: </label>
                      <input type="text" name="forwhom" id="forwhom">
                  </li>
                  <li>
                      <label for="email">Email: </label> 
                      <input type="text" name="importance" id="importance"> 
                  </li>
                  <li>
                      <label for="message">Your Message: </label><br>
                      <textarea name="message" id="message"></textarea>
                  </li>
                  <li>
                      <input type="submit" value="Go!">
                  </li>
              </ul>
          <?php if(isset($status)): ?>
          <p><?= $status; ?></p>
          <?php endif; ?>
          </form>
          </body>
          </html>
      

      I added a form just for explanation sake, hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效