简体   繁体   中英

Can't get PHP script to write user input to text file

I cannot get this simple PHP script to grab the data from the HTML form. The script throws this warning:

(Notice: Use of undefined constant 'email' - assumed ''email'' in /absolute/fullpath/godaddy/frustration/html/mail.php on line 5)

Longer explanation: I am writing this simple PHP script for my buddy's website. It is supposed to collect the email address and write it to a text file. I decided to go this route instead of utilizing any type of database. I have the email.txt file and PHP script both on the root directory of the webserver. We're using GoDaddy to host the site. I made sure GoDaddy had PHP enabled and that no restrictive settings were present.

I changed the permissions of both the script and the text file to 666 . I've concluded that the script doesn't grab the data that is being posted from the HTML form. I came to this conclusion because I tested the fwrite function and it easily wrote to the text file (email.txt) without a hiccup.

I've spent only a few hours over the past two weeks trying to complete this, but I feel like I have exhausted all of my options. I've searched far and wide through Google and noticed other people with similar issues. However, they fixed their issue by adding full-paths for the text file, changing permissions, etc.

Whenever I input an email address into the textbox and hit submit nothing is written to the email.txt file, just an empty text file.

Thank you in advance for any feedback or help. It's much appreciated!

<form id="emaillist" name="email" method="post" action="mail.php">
<label for="mailList"></label>
<input type="text" name="email" id="email" placeholder="Email Address" />
<input type="submit" name="submitMail" id="submitMail" value="Submit" />
</form>

<?php
ini_set('display_errors', 1); // show errors
error_reporting(-1); // of all levels
date_default_timezone_set('UTC'); // PHP will complain about on this error level
$emailaddr = $_POST[’email’];
$fp = fopen('/full/path/totextfile/html/email.txt', 'a');
fwrite($fp, $emailaddr);
fclose($fp);
echo 'Thank you!';
phpinfo();
?>

Correct:

$emailaddr = $_POST[’email’]; // back ticks are used to enclose, incorrect.

To:

$emailaddr = $_POST['email']; // single quotes are used to enclose, correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM