我需要将表单输入保存到服务器上的文本文件中。我在服务器"email.txt"上创建了一个文本文件,并授予它777的权限。但是,当提交表单时,文本文件将保持空白。
我的html如下:
<form action="process.php" method="post">
<input id="email-input" type="text" name="your-email" placeholder="you@yourmail.com" class="cform-text" size="65" title="your email">
<input id="optin-button" type="submit" value="Download The Report" class="cform-submit">
</form>
Php如下:
<?PHP
$email = $_POST["email-input"];
$to = "you@youremail.com";
$subject = "New Email Address for Mailing List";
$headers = "From: $email'n";
$message = "A visitor to your site has sent the following email address to be added to your mailing list.'n
Email Address: $email";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: you@youremailaddress.com'n";
$usermessage = "Thank you for subscribing to our mailing list.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
$fh = fopen("email.txt", "a");
fwrite($fh, $email);
fclose($fh);
header("Location: mysite.com");
?>
请协助。感谢
您的(电子邮件)输入带有id="email-input"
的id
,但它被"命名"为name="your-email"
,与您的POST变量不匹配。
更改:
$email = $_POST["email-input"];
至:
$email = $_POST["your-email"];
您不能依赖id
,而只能依赖元素的name
,这就是文件为空的原因。
使用错误报告会发出错误信号。
注意:
我建议您将fwrite($fh, $email);
更改为fwrite($fh, $email . "'n");
,否则,您将把所有累积的电子邮件地址都放在一条连续的线上。
将错误报告添加到文件顶部,这将有助于查找错误。
error_reporting(E_ALL);
ini_set('display_errors', 1);
旁注:错误报告只能在临时阶段进行,而不能在生产阶段进行。
使用php中提供的file_put_contents(文件、数据、模式、上下文)。
来源:http://www.w3schools.com/php/func_filesystem_file_put_contents.asp
如果您想将文本附加到文件中已有的文本,请在模式中使用file_append,因此它看起来像这样:
file_put_contents("email.txt",$email,FILE_APPEND);