【问题标题】:HTML form submit content with PHP to .txt file - blank? [closed]HTML 表单使用 PHP 将内容提交到 .txt 文件 - 空白? [关闭]
【发布时间】:2015-02-05 00:34:45
【问题描述】:

我创建了一个 HTML 表单,它应该使用 PHP 将输入的文本提交到一个 .txt 文件中,但它只创建了一个空行,我不知道问题是什么。

非常感谢您的帮助!

<form class="form-inline validate" name="form1" method="post" action="signup.php" target="_blank" novalidate>
    <div class="row no-gutter">
        <div class="col-sm-9">
            <input type="email" value="" name="mail" class="form-control" placeholder="Subscribe to our newsletter" required>
        </div>
        <div class="col-sm-3">
            <input type="submit" value="SUBSCRIBE" name="Submit">
        </div>

    </div>
</form>

<?php

$username = $_POST['user'];

//the data

$data = "$email\n";

//open the file and choose the mode

$fh = fopen("users.txt", "a");
fwrite($fh, $data);

//close the file

fclose($fh);
print "User Submitted";

?>

【问题讨论】:

  • 首先要做的是添加一个&lt;input name="user"&gt; 元素(或者不寻找$_POST['user'])。您还应该(可能)从$_POST['mail'] 获得$email。理解你的代码有点困难
  • 根据@Phil 所说,如果您的整个代码都在同一个文件中,请将您的可执行代码包装在条件语句中,isset() 以及您命名的提交按钮和empty() 用于输入。这样做并在页面加载时,将立即使用空白条目写入您的文件。
  • 在您打开 &lt;?php 标签 error_reporting(E_ALL); ini_set('display_errors', 1); 之后将错误报告添加到文件顶部,这将触发未定义索引警告。另外,您还使用了错误的变量来编写。 $username = $_POST['user'];$data = "$email\n";
  • 教人钓鱼……

标签: php html forms


【解决方案1】:

如果你想收到邮件,请将你的 php 代码更改为

   <?php
    if(isset($_POST['Submit'])){
       $email = $_POST['mail'];

      //the data

      $data = "$email\n";

      //open the file and choose the mode

      $fh = fopen("users.txt", "a+");
      fwrite($fh, $data);

      //close the file

      fclose($fh);
      print "User Submitted";
    }
    ?>

【讨论】:

  • 这会有所帮助,但条目之间仍然会有一个空行,因为 PHP 代码在表单显示和提交时执行。
猜你喜欢
  • 2012-11-28
  • 2011-06-13
  • 2013-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多