Forum Moderators: coopster

Message Too Old, No Replies

New to PHP and need a little help

         

pcguy64

9:21 pm on Sep 27, 2005 (gmt 0)



I am new to PHP and have been doing some reading and searching to try and get this correct, but I am running short on time to get it working properly. It is not quite finished for I still have to enter the code to email the people when they subscribe or unsubscribe. The problems I am having is getting the unsubscribe to work properly, getting the "Thank you for joining" from showing up in the other messages, getting rid of the extra character at the end of the email address in the .txt file and making sure that when people unsubscribe they are removed form the .txt file. If there is anything else that missed please let me know. The code below is what I currently have and the display messages and file locations are generic for testing purposes. Some of the code I have written myself and some of it I have gotten from reading various posts and tutorials. This is for a form that is on a website for users to subscribe/unsubscribe their email address for a newsletter.

<?php

$subscribe = $_GET['subscribe'];
$unsubscribe = $_GET['unsubscribe'];
$address = $_POST['address'];

if ($subscribe = "subscribe")
{
if (ereg('^[a-zA-Z0-9 \._\-]+@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]+$', $address))
{
echo ("<HTML><BODY>Thank you for joining.</BODY></HTML>");
}
else
{
echo ("<HTML><BODY><b>$address</b> &nbsp;not a valid email address. Please try again.</BODY></HTML>");
}
if(file_exists("email_list.txt"))
{
$newfile = fopen("email_list.txt", "r");
while(!feof($newfile))
{
$duplicate = fgetss($newfile, 255);

if(eregi("$address", $duplicate))
{
echo ("<HTML><BODY><b>$address</b>&nbsp;is already in the database.</BODY></HTML>");

fclose($newfile);
exit;
}
}
fclose($newfile);
$addemail = fopen("email_list.txt", "a");
fputs($addemail, "$address\n");
fclose($addemail);
}
else
{

$newfile = fopen("email_list.txt", "a");
fputs($newfile, "$address\n");
fclose($newfile);
}

if ($unsubscribe = "unsubscribe")
{
$emails = file("email_list.txt");
for($index = 0; $index < count($emails); $index++)
{
if(ereg("$unsubscribe", $emails[$index]))
{
$emails[$index] = "";
$thefile = fopen("email_list.txt", "w");
for($newindex = 0; $newindex <count($emails); $newindex++)
{
if($emails[$newindex]!= "")
{
fputs($thefile, "$emails[$newindex]");
}
}
fclose($thefile);
echo("<HTML><BODY><b>$address</b>&nbsp;has successfully been removed from the database.</BODY></HTML>");
}
}
}
}
?>

coopster

11:58 pm on Sep 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, pcguy64.

That's quite a mouthful ;) Let's start with first things first ... unsubscribe.

In your code you are assigning a value again to the $unsubscribe variable, not comparing it. So at the top of your code you are getting the value of the GET variable named 'unsubscribe' and assigning it to the variable '$unsubscribe'.

$unsubscribe = $_GET['unsubscribe'];

But then later in the code you are assigning the constant value 'unsubscribe' to the variable, overwriting what was originally stored from your GET variable.
if ($unsubscribe = "unsubscribe")

Are you sure you want to be doing this? Assignment is a single equal sign whereas comparison requires double equal signs.
if ($unsubscribe == "unsubscribe")

Assignment Operators [php.net]
Comparison Operators [php.net]

pcguy64

1:40 pm on Oct 3, 2005 (gmt 0)



I have tried that and still not getting anywhere.

coopster

12:40 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's just the start. You also have a similar problem with the subscribe, did you fix that too?
if ($subscribe = "subscribe")

pcguy64

5:48 pm on Oct 5, 2005 (gmt 0)



I finally got it figured out and working...thanks. Now I just have to add the mail() part of the script in so those that subscribe / unsubscribe will get an email notification and make sure that it works. Guess I got in a little over my head when I took on this project. Thanks for the help.

coopster

6:00 pm on Oct 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




Guess I got in a little over my head

Naw. You handled it, so congratulations. We all have to start somewhere!