help with email verification

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lscueto
Forum Newbie
Posts: 3
Joined: Tue Jul 01, 2008 8:25 am

help with email verification

Post by lscueto »

Im quite new with php and I need a little help. I have this code that verifies email and saves it into a database. My problem is after its verified the $email variable becomes empty and saves a blank("") in the email field in the database. Pls tell me what's wrong with the code. Any help would be very much appreciated.

Thanks,
Lscueto



<?

include "db.php";
$email=$_POST['email'];

function EmailValidation($email) {
$email = htmlspecialchars(stripslashes(strip_tags($email))); //parse unnecessary characters to prevent exploits

if ( eregi ( '[a-z||0-9]@[a-z||0-9].[a-z]', $email ) ) { //checks to make sure the email address is in a valid format
$domain = explode( "@", $email ); //get the domain name

if ( @fsockopen ($domain[1],80,$errno,$errstr,3)) {
//if the connection can be established, the email address is probabley valid


return true;
/*

GENERATE A VERIFICATION EMAIL

*/

} else {
return false; //if a connection cannot be established return false
}

} else {
return false; //if email address is an invalid format return false
}
}

function EmailForm($email){
if(empty($_POST['email'])){
echo "<form action=".$_SERVER['PHP_SELF']." method='post'>
<table border='0'>
<tr>
<td>Email</td>
<td><input name='email' type='text' id='email' /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type='submit' name='Submit' value='Subscribe' /></td>
</tr>
</table>
</form>";
} elseif(isset($_POST['email'])) {

if(EmailValidation($_POST['email'])) {


$sql=mysql_query("INSERT INTO mailinglist (email,date_joined) VALUES ('$email',now())")or die(mysql_error());
if(!$sql)
{
echo " Data not uploaded pls check your inputs";
}else{
echo " You are now subscribed";
}



} else {
echo "Your email address appears to be invalid. Please try again.";
}

} else {

echo "An error has occured, please contact the administrator.";

}
}

EmailForm();

?>
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: help with email verification

Post by jaoudestudios »

in your mysql query instead of using $email, why dont you use $_POST['email'] that way you know it will contain the email address as with $email if you dont make it global within a function you cant use it outside a function.

when you are calling emailform() at the end of the script you are not entering a variable, I am surprised you are not getting an error message, because in your function declaration you are not using a fail safe like function emailform ($email='') { ...

conclusion
use $_POST['email']

hope that helps
Eddie
lscueto
Forum Newbie
Posts: 3
Joined: Tue Jul 01, 2008 8:25 am

Re: help with email verification

Post by lscueto »

Hi Sir,

Thank you for your quick response. Just like to ask how do I do that? sorry for the ignorance. I tried it like

$sql=mysql_query("INSERT INTO mailinglist (email,date_joined) VALUES ($_POST['email'],now())")or die(mysql_error());


And

like this

$sql=mysql_query("INSERT INTO mailinglist (email,date_joined) VALUES ('$_POST['email']',now())")or die(mysql_error());

but its giving me the error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/lscueto/public_html/email.php on line 114

Hope you can help me. Thanks so much.

lscueto
lscueto
Forum Newbie
Posts: 3
Joined: Tue Jul 01, 2008 8:25 am

Re: help with email verification

Post by lscueto »

Hi,

its working now. Thanks so much for the help. more power to you.

Thanks,
LSCueto
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: help with email verification

Post by jaoudestudios »

:lol: thanks
Post Reply