tqla Posted July 5, 2007 Share Posted July 5, 2007 Hi. I am trying to compare two email fields and declare them "$bad_email_format[] = $field;" if they do not match. This looks right to me but it doesn't work. Where am I going wrong? elseif ($field == "email" ) { if ('email' != 'confirmEmail' ) { $bad_email_format[] = $field; } } Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 5, 2007 Share Posted July 5, 2007 What is this line? if ('email' != 'confirmEmail' ) Shouldn't "email" and "confirmEmail" be post data variables from what the user filled out? Maybe something like this... if ($_POST['email'] != $_POST['confirmEmail']) Could you post more code so we can see what all the form values/information should be? Quote Link to comment Share on other sites More sharing options...
tqla Posted July 5, 2007 Author Share Posted July 5, 2007 Hi pocobueno1388. Come to think of it, I am not using $_POST['data'] at all and everything works except the 'email/confirmEmail' comparison. It shows the error message "Emails do not match" no matter if they do or don't. Here's the entire script: <?php session_start(); require_once('db/db.php'); ?> <?php /* set up array containing all the fields */ $labels = array ( "clubCard" => "Club Card Number", "firstName" => "First Name", "lastName" => "Last Name", "address" => "Address", "additionalAddress" => "Additional Address", "city" => "City", "state" => "State", "zip" => "Zip Code", "email" => "Email Address", "confirmEmail" => "Confirm Email Address", "okToContact" => "OK To Contact"); foreach ($_POST as $field => $value) { /* check each field for blank fields */ if ( $value == "" ) { if ($field != "clubCard" and $field != "additionalAddress" and $field != "okToContact" ) { $blank_array[] = $field; } } /* check text for invalid formats. */ elseif ($field == "address" or $field == "clubCard" or $field == "additionalAddress" ) { if (!ereg("^[A-Za-z0-9'.# -]{1,50}$",$_POST[$field]) ) { $bad_format[] = $field; } } /* check text for invalid formats. */ elseif ($field == "firstName" or $field == "lastName" or $field == "city" or $field == "state" ) { if (!ereg("^[A-Za-z'. -]{1,50}$",$_POST[$field]) ) { $bad_format[] = $field; } } /* check zip for invalid format. */ elseif ($field == "zip") { if(!ereg("^[0-9]{5,5}(\-[0-9]{4,4})?$",$value)) { $bad_format[] = $field; } } /* check email for invalid formats. */ elseif ($field == "email" ) { if ('email' != 'confirmEmail' ) { $bad_email_format[] = $field; } } } /* if any fields are not okay, display error message and form */ if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0 or @sizeof($bad_email_format) > 0) { if(@sizeof($blank_array) > 0) { /* display message for missing information */ $error_text .= "<b><font face=\"verdana\" color=\"#ff0000\">You didn't fill in one or more required fields. You must enter:</b><br> </font>"; $errors = 'yes'; /* display list of missing information */ foreach($blank_array as $value) { $error_text .= "<font face=\"verdana\" color=\"#ff0000\"> {$labels[$value]}<br></font>"; $errors = 'yes'; } } if(@sizeof($bad_format) > 0) { /* display message for bad information */ $error_text .= "<BR><font face=\"verdana\" color=\"#ff0000\"><b>One or more fields have information that appears to be incorrect. Correct the format for:</b><br></font>"; $errors = 'yes'; /* display list of bad information */ foreach($bad_format as $value) { $error_text .= "<font face=\"verdana\" color=\"#ff0000\"> {$labels[$value]}<br></font>"; $errors = 'yes'; } } if(@sizeof($bad_email_format) > 0) { /* display message for bad information */ $error_text .= "<BR><b><font face=\"verdana\" color=\"#ff0000\">Emails do not match</b><br></font>"; $errors = 'yes'; /* display list of bad information */ foreach($bad_email_format as $value) { $error_text .= "<font face=\"verdana\" color=\"#ff0000\"> {$labels[$value]}<br></font>"; $errors = 'yes'; } } /* redisplay form */ include("page1.php"); exit(); } /* if data is good */ $_SESSION['clubCard'] = $clubCard; $_SESSION['firstName'] = $firstName; $_SESSION['lastName'] = $lastName; $_SESSION['address'] = $address; $_SESSION['additionalAddress'] = $additionalAddress; $_SESSION['city'] = $city; $_SESSION['state'] = $state; $_SESSION['zip'] = $zip; $_SESSION['email'] = $email; $_SESSION['confirmEmail'] = $confirmEmail; $_SESSION['okToContact'] = $okToContact; header("Location: nextpage.php"); ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 /* check email for invalid formats. */ elseif ($field == "email" ) { if ('email' != 'confirmEmail' ) { $bad_email_format[] = $field; } } } thats what hes saying sure always ! if ('email' != 'confirmEmail' ) Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 5, 2007 Share Posted July 5, 2007 It's past my bedtime but if ('email' != 'confirmEmail' ) will always be true as the string email never equals the string confirmEmail. It's not a variable comparison, it's a string comparison. Quote Link to comment Share on other sites More sharing options...
tqla Posted July 5, 2007 Author Share Posted July 5, 2007 Wait a minute! Are you saying that I first need to change email and confirmEmail to variables like this? $email = email $confirmEmail = confirmEmail And THEN compare them? Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 5, 2007 Share Posted July 5, 2007 yes and no note variable should dynamic that will satisfy the code objective Quote Link to comment Share on other sites More sharing options...
tqla Posted July 5, 2007 Author Share Posted July 5, 2007 I just tried Poco's suggestion of using if ($_POST['email'] != $_POST['confirmEmail']) and it WORKS! Thanks pocobueno1388! Thanks everyone else too! This board is a life saver. Quote Link to comment Share on other sites More sharing options...
tqla Posted July 5, 2007 Author Share Posted July 5, 2007 One more thing. Teng84 and AndyB, I see what you mean. My comparison was not a comparison at all, but rather a true statement the said two words were not the same. I'm such a dummy sometimes. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.