HOME       >>       Programming

E-mail List Error


XIII

I just coded this e-mail list, it works well for entering data into database, but if user leaves the field blank or adds an already exist e-mail it gives him the error message and stops loading the rest of the page.

 

<?php// Connects to your Databasemysql_connect("localhost", "my_username", "mypassword") or die(mysql_error());mysql_select_db("Database name") or die(mysql_error());//This code runs if the form has been submittedif (isset($_POST['submit'])) {//This makes sure they did not leave any fields blankif (!$_POST['email']) {die('<font color=red><b><i>No e-mail added</i></b>');}// checks if that e-mail is in useif (!get_magic_quotes_gpc()) {$_POST['email'] = addslashes($_POST['email']);}$mailcheck = $_POST['email'];$check = mysql_query("SELECT email FROM maillist WHERE email = '$mailcheck'")or die(mysql_error());$check2 = mysql_num_rows($check);//if e-mail exists it gives an errorif ($check2 != 0) {die('<font color=red><b><i>Already subscribed</i></b>');}else{echo"<font color=blue><b><i>Subscribed successfully</i></b>";}// now we insert it into the database$insert = "INSERT INTO maillist (id, email) VALUES ('0', '".$_POST['email']."')";$add_email = mysql_query($insert);?><?php}else{?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><table border="0"><tr bgcolor="#6652D8"> <td width="140"><span ><span >Get our Newsletter</span>: </span></td> </tr><tr><td><input type="text" name="email" maxlength="100"></td></tr><tr><th colspan=2><input type="submit" name="submit" value="Subscribe"></th></tr><tr><td></td></tr><tr><td bgcolor="#6652D8"></td></tr></table></form><?php}?>

1- i need to make it say the error message and continue loading the rest of the page.

2- checks for the entered text "if it's a valid e-mail format xxxxxx@xxxx.xxxx."


Hercco

1- i need to make it say the error message and continue loading the rest of the page.



Don't use the die() function then. It does exactly what the name suggest: makes the script commit suicide and print out the error message you specified as it's last words. Simply print out the error in a conventional way ( echo, printf) and make the script not to perform the other subscription steps (such as adding the email to the database).

2- checks for the entered text "if it's a valid e-mail format xxxxxx@xxxx.xxxx."[/color]


For that you need to use a regular expression. There's thousands of examples available online. Here's one thread about the subject: http://forums.xisto.com/topic/80809-topic/?findpost=1064280124

XIII

Don't use the die() function then. It does exactly what the name suggest: makes the script commit suicide and print out the error message you specified as it's last words. Simply print out the error in a conventional way ( echo, printf) and make the script not to perform the other subscription steps (such as adding the email to the database).

That's already what i need, i can't get a function to do that, i need a function to show the error message and stop loading the script while loading the rest of the page.

 

For that you need to use a regular expression. There's thousands of examples available online. Here's one thread about the subject: http://forums.xisto.com/topic/80809-topic/?findpost=1064280124

 


Thank you so much, this link helped me so much, specially Houdini's post, he is really a php guru.

TavoxPeru

I hope that the following code help you, is a registration and activation page, its very simple and self explanatory, if you dont understand something please send me a PM.

<?php /*** Generate an activation code* @param int $length is the length of the activation code to generate* @return string*/function generateCode($length = 10){ $password=""; $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; srand((double)microtime()*1000000); for ($i=0; $i<$length; $i++) { $password .= substr ($chars, rand() % strlen($chars), 1); } return $password; } // config file with database info (db, host, user, password) and connection functions.require("config.php");?><HTML><HEAD><TITLE>Registration http://forums.xisto.com/no_longer_exists/ HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><style type="text/css">body { background-color:#131313; font-family:Verdana, Arial; font-weight:bold; font-size:9px; color:#FFFFFF;}.register_box { border: 1px solid #323232; background-color: #202020; font-family: Verdana, Arial; font-weight: bold; font-size: 9px; color: #FFFFFF; } </style></head><body><?php$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "new";switch($action) { //-------------------------------------- // [New Registration] //-------------------------------------- case "new": if(!isset($_POST['register'])) { echo " <form action='registration.php' method='POST'> Name: <br /> <input type='text' name='name' class='register_box'> <br /> Email: <br /> <input type='text' name='email' class='register_box'> <br /> Nickname: <br /> <input type='text' name='nickname' class='register_box'> <br /> Password: <br /> <input type='password' name='password' class='register_box'> <br /> <input type='submit' name='register' value='New Registration!' class='register_box'> <input type='hidden' name='action' value='new'> </form>"; } elseif(isset($_POST['register'])) { $name = mysql_real_escape_string($_POST['name']); $email = mysql_real_escape_string($_POST['email']); $nickname = mysql_real_escape_string ($_POST['nickname']); $password = mysql_real_escape_string($_POST['password']); $activation_code = generateCode(25); $nameq = "SELECT name FROM registration WHERE name = '$name'"; $emailq = "SELECT email FROM registration WHERE email = '$email'"; $nickq = "SELECT nickname FROM registration WHERE nickname = '$nickname'"; //put errors into an array I need to change these if I change the db $errors = array(); if(empty($name)) { $errors[] = "The name field was blank! <br />"; } $reg=mysql_query($nameq) or die(mysql_error()); if(mysql_num_rows($reg) > 0) { $errors[] = "The name given is already in use! Please try another one! <br />"; } if(empty($email)) { $errors[] = "The email field was blank! <br />"; } $reg=mysql_query($emailq) or die(mysql_error()); if(mysql_num_rows($reg) > 0) { $errors[] = "The email given is already in use! Please try another one! <br />"; } if(empty($nickname)) { $errors[] = "The nickname field was blank! <br />"; } $reg=mysql_query($nickq) or die(mysql_error()); if(mysql_num_rows($reg)>0) { $error[] = "That nickname is already taken. Please try another one.<br />"; } if(empty($password)) { $errors[] = "The password field was blank! <br />"; } if(count($errors) > 0) { foreach($errors as $err) { echo $err; } } else { $sqlq = "INSERT INTO registration (name, email, nickname, password, is_activated, activation_code)"; $sqlq .= " VALUES ('$name', '$email', '$nickname', '".md5($password)."', '0', '$activation_code')"; mysql_query($sqlq) or die(mysql_error()); echo "Thanks for registering! You will recieve an email shortly containing your validation code, and a link to activate your account!"; mail($email, "New Registration, gigasoft.astahost.com;, "Thanks for registering on TRIVIA at GIGASOFT.\n\nHere are your login details:\n\nNickname: ".$nickname."\n\nPassword: ".$password."\n\nIn order to login and gain full access, you must validate your account.\n\nClick here to validate:\n\nhttp://forums.xisto.com/no_longer_exists/trivia/registration.php?action=activate&nickname;; } } break; case "activate": //-------------------------------------- // [Activate Account] //-------------------------------------- if(isset($_GET['nickname']) && isset($_GET['code'])) { $nickname = mysql_real_escape_string($_GET['nickname']); if(mysql_num_rows(mysql_query("SELECT email FROM registration WHERE nickname = '$nickname'")) == 0) { echo "That username is not in the database!"; } else { $activate_query = "SELECT is_activated FROM registration WHERE nickname = '$nickname'"; $is_already_activated = mysql_fetch_object(mysql_query($activate_query)) or die(mysql_error()); if($is_already_activated->is_activated == '1') { echo "This user is already activated!"; } else { $code = mysql_real_escape_string($_GET['code']); $code_query = "SELECT activation_code FROM registration WHERE nickname = '$nickname' LIMIT 1"; $check_code = mysql_fetch_object(mysql_query($code_query)) or die(mysql_error()); if($code == $check_code->activation_code) { $update = "UPDATE registration SET is_activated='1', regdate=NOW(), last_login=NOW() WHERE nickname = '$nickname'"; mysql_query($update) or die(mysql_error()); echo "User $nickname has been activated! Thanks!"; echo "<br />Please <a href=\"login.php\">Click here to Login</a> and enter the site."; } else { echo "The activation code was wrong! Please try again!"; } } } } else { echo "No ID or user given to activate!"; } break; }?> </body></html>

Best regards,

XIII

Thank you so much, i respect your effort to paste this registeration system here though in fact i do need only to fix some problems in my own script, i already fixed some of them, so i will paste the new edited code, i hope to find some help fixing the rest of my code errors:

 

<?php/// Connects to your Databasemysql_connect("localhost", "my username", "my password") or die(mysql_error());mysql_select_db("my db name") or die(mysql_error());///This code runs if the form has been submittedif (isset($_POST['submit'])) {///This makes sure they did not leave any fields blankif (!$_POST['email']){[COLOR=Red]die[/COLOR]"<font color=red><b><i>No e-mail added</i></b>";}/// checks if that e-mail is in useif (!get_magic_quotes_gpc()) {$_POST['email'] = addslashes($_POST['email']);}$mailcheck = $_POST['email'];$check = mysql_query("SELECT email FROM maillist WHERE email = '$mailcheck'")or die(mysql_error());$check2 = mysql_num_rows($check);///if e-mail exists it gives an errorif ($check2 != 0) {echo"<font color=red><b><i>Already subscribed</i></b>";}else{$insert = "INSERT INTO maillist (id, email) VALUES ('0', '".$_POST['email']."')";$add_email = mysql_query($insert);echo"<font color=blue><b><i>Subscribed successfully</i></b>";}///now we insert it into the database?><?php}else{?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"><table border="0"><tr bgcolor="#6652D8"> <td width="140"><span ><span >Get our Newsletter</span>: </span></td> </tr><tr><td><input type="text" name="email" maxlength="100"></td></tr><tr><th colspan=2><input type="submit" name="submit" value="Subscribe"></th></tr><tr><td></td></tr><tr><td bgcolor="#6652D8"></td></tr></table></form><?php}?>

still that red die above in the code stop loading of the whole page, and if i use print or echo, it will tell the error message but will not prevent inserting to database, the above red die in :

 

///This makes sure they did not leave any fields blankif (!$_POST['email']){[COLOR=Red]die[/COLOR]"<font color=red><b><i>No e-mail added</i></b>";}

stops all the page from continue loading, when i used echo instead, it didn't prevent inserting a blank row to database.

 

the second problem is, i have another form in the same page, when i press submit, it excute both scripts, what should i do?



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.