longsack Posted September 6, 2017 Share Posted September 6, 2017 Sorry guys, I'm just learning, i'm sure this is simple....My script is basically, submit your email address, write the email address to a text file... simple! I want to check the email address submitted against the text file and if the email address is already located in the text file... display a message "already added"... Here is what I have so far... very simple.... <?php if(isset($_POST['submit'])) { $email = $_POST['email'] . PHP_EOL; $file = fopen("emails.txt","a+"); fwrite($file,$email); fclose($file); print_r(error_get_last()); } ?> <form action= "" method="post" name="form"> <input type="text" name="email"> <br> <br> <input type="submit" name="submit" value="submit"><br> </form> any thoughts on how to make this happen? Quote Link to comment Share on other sites More sharing options...
Sepodati Posted September 6, 2017 Share Posted September 6, 2017 Before you do your writing, get all of the file contents with file_get_contents() and then use strstr() to see if $_POST['email'] is in the contents you read. Only if it isn't, then open and write the email address to the file. Quote Link to comment Share on other sites More sharing options...
longsack Posted September 9, 2017 Author Share Posted September 9, 2017 ya, i know what your saying, never wrote something like that before (very new), so will have to do some research it seems and see if I can figure it out. Quote Link to comment Share on other sites More sharing options...
gizmola Posted September 9, 2017 Share Posted September 9, 2017 It's really not that complicated -- a small addition to your existing code: $filename = 'emails.txt'; if(isset($_POST['submit'])) { $email = trim($_POST['email']); $fileString = file_get_contents($filename); // Check to see if email is already in file. if (false === stristr($fileString, $email)) { $file = fopen($filename, "a+"); fwrite($file, $email . PHP_EOL); fclose($file); echo "Added Email: $email to the system"; } else { echo "Email: $email already exists in the system.\n"; } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.