Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/206095-form-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.