Reply
If statement problem
Old 04-30-2008, 02:42 PM If statement problem
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
Hi,

I want to check the user has entered a valid email, but allow them to submit the form if the field is empty (4 textboxes can be used but user doesn't have to use all of them)

I can't see why the following code doesn't work, any suggestions?

PHP Code:
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email)){ 
$msg=$msg."<img src='error.png'> Invalid email address.<BR>";
  if(
$email==""){
  
$status="OK";} else {
$status"NOTOK";}} 
__________________
www.hotlista.co.uk
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
When You Register, These Ads Go Away!
Old 04-30-2008, 02:52 PM Re: If statement problem
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 738
You really should indent your code, it'll save you a lot of problems in the future. Your logic was incomplete and mixed around as well. You need to check for every possibility. Try this:

PHP Code:
$email_regex "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if (
$email == '') {
     
$status 'OK';
} else {
      if (!
eregi($email_regex$email)) {
          
$status 'NOT OK';
     } else {
          
$status 'OK';
     }

VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 04-30-2008, 03:16 PM Re: If statement problem
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
I tried using that code but it doesn't seem to execute it correctly, I want to ensure there is at least one email address entered by the user, if it is empty set to NOTOK, if an email address has been entered check eregi and send if OK. Really can't see why this isn't working and have been trying for ages to get it to work

PHP Code:
 
$status
="OK";
 
$email_regex "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if(
$email1=="") {
     
$msg=$msg."<img src='error.png'> You must enter at least one email address<BR>";
     
$status "NOTOK";
} else {
if(!
eregi($email_regex$email1)) {
$status "NOTOK";}}
 
if (
$email2 == "") {
     
$status "OK";
} else {
      if (!
eregi($email_regex$email2)) {
          
$status "NOTOK";}}
 
email 3 etc
 
email 4 etc 
__________________
www.hotlista.co.uk

Last edited by drew22299 : 04-30-2008 at 03:18 PM.
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 04-30-2008, 06:07 PM Re: If statement problem
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 738
Try this one instead. I haven't tested it completely, but it should work. It only triggers $status if one of the email fields has been filled out correctly. It also has a security check to prevent SQL injection.

PHP Code:

$email_regex 
'\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b';
$status 'NOTOK';

if (
$email1 != '') {
     if (
preg_match("#$email_regex#"$email1)) {
          
$email1 mysql_escape_string(trim($email1);
          
$status 'OK'
     
}
}

if (
$email2 != '') {
     if (
preg_match("#$email_regex#"$email2)) {
          
$email2 mysql_escape_string(trim($email2);
          
$status 'OK'
     
}
}

if (
$status == 'NOTOK') {
     
$msg 'Your error message here';
} else if (
$status == 'OK') { //Status is OK, do what you need to do


VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 05-01-2008, 04:47 AM Re: If statement problem
drew22299's Avatar
Skilled Talker

Posts: 89
Location: Wiltshire, UK
Thanks for your help VirtuosiMedia

Your code looks like it should work and I think it does but when the code executes $status can get set to NOTOK and then later get set to OK again, I have spent hours on this now trying to get it to work lol

It should check each email address entered by the user, make sure there is at least one email address entered ($email1), check the email address isn't already registered to an account, display error messages if the first email has not been entered or if any of the other emails that have been entered have an invalid format.

I bet this is prob a really easy thing to do in PHP and I'm not getting the logic right, maybe if I used more $status checks like $email1status="NOTOK" etc

This is the code I'm using:
PHP Code:
 
$status 
"NOTOK";
$msg=""
if(
$email1==""){
$msg=$msg."<img src='error.png'> You must enter at least one email address<BR>";
$status"NOTOK";} 
 
if(!
email1==""){
if(!
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email2)){
$msg=$msg."<img src='error.png'> Invalid email address 2<BR>";
$staus="NOTOK";
}}
 
if(!
$email2=="" && !$email1==""){
if(!
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email3)){
$msg=$msg."<img src='error.png'> Invalid email address 3<BR>";
$staus="NOTOK";
}}
 
if(!
$email1=="" && !$email2=="" && !email3==""){
if(!
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email4)){
$msg=$msg."<img src='error.png'> Invalid email address 4<BR>";
$staus="NOTOK";
}}
 
if(!
$email1==""){
if(!
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$email1)){
$msg=$msg."<img src='error.png'> Invalid email address 1<BR>";
$staus="NOTOK";}
else {
$staus="OK";}}
if(
mysql_num_rows(mysql_query("SELECT * FROM users WHERE email = '$email1'"))){
$msg=$msg."$email1 is already registred to an account<BR>";
$status"NOTOK";}
if(
mysql_num_rows(mysql_query("SELECT * FROM users WHERE email = '$email2'"))){
$msg=$msg."$email2 is already registred to an account<BR>";
$status"NOTOK";}
if(
mysql_num_rows(mysql_query("SELECT * FROM users WHERE email = '$email3'"))){
$msg=$msg."$email3 is already registred to an account<BR>";
$status"NOTOK";} 
if(
mysql_num_rows(mysql_query("SELECT * FROM users WHERE email = '$email4'"))){
$msg=$msg."$email4 is already registred to an account<BR>";
$status"NOTOK";}   
if(
$status<>"OK"){ 
echo 
"<font face='arial' color=red>$msg<center> <img src='error.png'> Invites were not sent </font><br><br><input type='button' value='Go back' onClick='history.go(-1)'></center>";
}else{ 
// if all validations are passed.
//$query=mysql_query("select * from plus_signup where userid = '$userid)");
echo "<font face='arial' color=green><br><img src='success.png'> Click the button below to send invites.<br><br></font>"
__________________
www.hotlista.co.uk

Last edited by drew22299 : 05-01-2008 at 04:48 AM.
drew22299 is offline
Reply With Quote
View Public Profile Visit drew22299's homepage!
 
Old 05-01-2008, 01:21 PM Re: If statement problem
VirtuosiMedia's Avatar
Webmaster Talker

Posts: 738
Oops, I forgot about the invalid check. I'm changing this a little by adding the error array, but hopefully you'll see how it could benefit you. I haven't tested this, but this should work without needing to make any changes:

PHP Code:
//I've tested this regex format, so I know it works like this
$email_regex '\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b';

//This is our check to make sure at least one form is filled out
$email_status FALSE;

//This is the errors array, initialize it to NULL 
$errors = array ();

if (
$email1 != '') {
     if (
preg_match("#$email_regex#"$email1)) {
          
$email1 mysql_escape_string(trim($email1);
          
$email_status TRUE;
     } else {
          
$errors[] = 'You must enter a valid email format for email #1';
     }


if (
$email2 != '') {
     if (
preg_match("#$email_regex#"$email2)) {
          
$email2 mysql_escape_string(trim($email2);
          
$email_status TRUE;
     } else {
          
$errors[] = 'You must enter a valid email format for email #2';
     }


if (
$email3 != '') {
     if (
preg_match("#$email_regex#"$email3)) {
          
$email3 mysql_escape_string(trim($email3);
          
$email_status TRUE;
     } else {
          
$errors[] = 'You must enter a valid email format for email #3';
     }
}

if (
$email4 != '') {
     if (
preg_match("#$email_regex#"$email4)) {
          
$email4 mysql_escape_string(trim($email4);
          
$email_status TRUE;
     } else {
          
$errors[] = 'You must enter a valid email format for email #4';
     }
}

if (!
$email_status) { //If no email addresses have been entered
     
$errors[] = 'You must enter at least one email address';
}

if (empty(
$errors)) { //If no errors have occurred
      //Enter your code here
} else { //Errors have occurred, display them

      
echo "<ul>";
      foreach (
$errors as $error)    {
           echo 
"<li$class>".$error.'</li>';
      }   
      echo 
"</ul>";


I'm sure you probably have other fields that you need to validate. To add another error to the error array, just use the same format:

PHP Code:
 $errors[] = 'Your error message goes here.'
That should work, hopefully.

Last edited by VirtuosiMedia : 05-01-2008 at 01:23 PM.
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Reply     « Reply to If statement problem
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14180 seconds with 12 queries