将电子邮件表单提交到.txt文件将不起作用

时间:2015-03-21 20:12:28

标签: php html forms email

我创建了一个脚本,其中包含一个应该将人员电子邮件提交到.txt文件的表单,唯一的问题是.txt文件没有任何内容,调用该函数时它保持空白。 html文件和php文件都保存在同一个文件夹中,.txt文件名为formdata.txt。

Html代码:

<form name="newsletter-form" action="process-form-data.php" method="post" id="newsletter-form">
            <input type="email" name="newsletter-email" id="newsletter-email" class="form-control" placeholder="Enter Your Email" data-validate="validate(required, email)" />
            <input type="submit" id="newsletter-submit" class="btn" value="Notify Me" />
        </form>

Php代码名为process-form-data.php:

    <?php

// Receive form Post data and Saving it in variables
$email = $_POST['newsletter-email'];

// Write the name of text file where data will be store
$filename = "formdata.txt";

// Marge all the variables with text in a single variable. 
$f_data= '
Email :  '.$email.'
=========================
';

echo 'Form data has been saved to '.$filename.'  <br>
<a href="'.$filename.'">Click here to read </a> ';
$file = fopen($filename, "a");
fwrite($file,$f_data);
fclose($file);
?>

1 个答案:

答案 0 :(得分:1)

您的代码适合我。

这是使用file_put_contents的变体:

// Receive form Post data and Saving it in variables
$email = $_POST['newsletter-email'];
//$email = 'myhappymail@unhappy.com';

// Write the name of text file where data will be store
$filename = "formdata.txt";

// Marge all the variables with text in a single variable. 
$f_data= '
Email2! :  '.$email.'
=========================
';
file_put_contents( $filename, $f_data, FILE_APPEND | LOCK_EX );

// $file = fopen($filename, "a");
// fwrite($file,$f_data);
// fclose($file);
echo 'Form data has been saved to '.$filename.'  <br>
<a href="'.$filename.'">Click here to read </a> ';

做一个:

var_dump( $_POST );
die;

位于脚本的顶部。我以为你错过了你的POST数据。