omahony Posted June 28, 2010 Share Posted June 28, 2010 I'm just learning PHP and I'm stuck on this problem... I'm trying to build a form where: 1. a user enters their email address. 2. the email is added to a text file. 3. the text file is searched to confirm that the email address has not been added previously. 4. if the email is unique a coupon is emailed to the user and the user is redirected to a thank you page. 5. if the email is already in the text file an error message is displayed. As of right now, everything is working except the part where the email address is written to the text file. If I manually add an email address to the text file in dreamweaver, the php page will check for it and return "this email already exists. But for some reason the php won't write to the file. I think I may just have some code in the wong order or something similar. Here is the code: <?php //post 'mail' to set variable $email $email = $_POST ['mail']; // THE FILE WRITING SECTION OF THE SCRIPT //set data variable to be written in file $data = "$email".","."\n"; $file = file_get_contents("emails.txt"); $pos = strpos($file, $email); //set variable to open txt file and chose mode $fh = fopen(emails.txt, a); //check text file for email if ($pos === false) { fwrite($fh, $data); //runs $fh and writes $data //close file fclose($fh); // THE EMAIL SECTION OF THE SCRIPT // define email sender, subject and message $from = "[email protected]"; $subject = "Your Free Game!"; $message = "<html> <body>email coupon </body> </html>"; //end of message //define headers $headers = "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; $headers .= "From: [email protected]\n"; $headers .= "Return-Path: [email protected]\n"; $headers .= "Message-Id: <".time()."@".$_SERVER['SERVER_NAME'].">\n"; $headers .= "X-Mailer: php-mail-function-0.2\n"; //send coupon to $email from:[email protected] mail($email, $subject, $message, $headers); //THE REDIRECT SECTION OF THE SCRIPT header( 'Location: http://www.website.com/thanks.html' ) ; } else { echo "this email already exists"; } ?> Any help or tips in general would be greatly appreciated. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/ Share on other sites More sharing options...
omahony Posted June 28, 2010 Author Share Posted June 28, 2010 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/#findComment-1078408 Share on other sites More sharing options...
conker87 Posted June 28, 2010 Share Posted June 28, 2010 Change $fh = fopen(emails.txt, a); to $fh = fopen("emails.txt", a); Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/#findComment-1078411 Share on other sites More sharing options...
omahony Posted June 28, 2010 Author Share Posted June 28, 2010 thanks, those quotes kill me sometimes... i made the change you suggested, but it still wont write the email address to the emails.txt file for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/#findComment-1078418 Share on other sites More sharing options...
omahony Posted June 28, 2010 Author Share Posted June 28, 2010 correction: sorry, now it will write to the file and send the email, but it isn't checking the text file to ensure that the email address hasn't been added.... thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/#findComment-1078419 Share on other sites More sharing options...
conker87 Posted June 28, 2010 Share Posted June 28, 2010 You could try imploding the data from the text file into an array then use array functions on them. Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/#findComment-1078424 Share on other sites More sharing options...
omahony Posted June 28, 2010 Author Share Posted June 28, 2010 eh i'm an idiot! its working beautifully now. i accidentally edited a copy of the php file, so the changes weren't working. once i realized what was going on and edited the correct code it started working. thank you so much for the help! Quote Link to comment https://forums.phpfreaks.com/topic/206095-form-help/#findComment-1078425 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.