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.

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

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序