PHP脚本无法正确写入文件

时间:2014-10-11 11:00:38

标签: php html

我最近开始研究PHP编码,到目前为止一切都很好,直到我尝试制作一个PHP脚本,将HTML表单中的数据存储到文本文件中。情况如下:在表单上有一个简单的文本框,在这种情况下,必须使用下面的提交按钮编写电子邮件地址,在PHP脚本上我有电子邮件:打印为文本而不是应该拉的变量从形式。文本电子邮件:已打印,但在表单中写入的实际地址无处可寻。下面是表单和脚本的代码。还有一件事,创建了写入信息的目标文本文件,因此脚本有权在目录中写入。服务器的php版本是5.2,apache版本是2.2.19(Unix)。我使用的主机服务是000webhost.com

表单代码:

<HTML>
<HEAD>
</HEAD>
<BODY>
<form action = "email_script2.php" method = "GET">
<p>Email Address: <input type = "text" name = "text" name = "email" size = "30"/> 
</p>
<input type = "submit" name = "submit" value = "Submit"/> 
</BODY>
</HTML>

脚本代码:

<?PHP
$email = $_GET['email'];
$file_handle = fopen("testfile.txt", "w");
$file_contents = "email:" . $email;
fwrite($file_handle, $file_contents);
fclose($file_handle);
ini_set('error_reporting', 'on'); error_reporting(E_ALL);
?>

提前致谢

1 个答案:

答案 0 :(得分:1)

您的输入文字应该只有一个&#34;名称&#34;属性如下:

<HTML>
<HEAD>
</HEAD>
<BODY>
<form action = "email_script2.php" method = "GET">
<p>Email Address: <input type = "text" name = "email" size = "30"/> 
</p>
<input type = "submit" name = "submit" value = "Submit"/> 
</BODY>
</HTML>

The text email: is printed but the actual address, written in the form is nowhere to be foud你是什么意思? 你的意思是你在表格中填写你的电子邮件而你的档案中什么都没有?

您可以尝试在屏幕上打印它以检查它是否具有正确的值:

<?PHP
$email = $_GET['email'];
echo $email // checking if it has correct value
$file_handle = fopen("testfile.txt", "w");
$file_contents = "email:" . $email;
fwrite($file_handle, $file_contents);
fclose($file_handle);
ini_set('error_reporting', 'on'); error_reporting(E_ALL);
?>
相关问题