HoTDaWg Posted March 13, 2007 Share Posted March 13, 2007 I know this is wrong, what would be the proper method for this?:S <?php $arrErrors= array(); if(strlen($email) > 50){ $arrErrors['email']=>'toolong'= "The E-mail Field exeeds the limit of <b>50</b> characters"; } ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 what are you going ot do with $arrErrors? you could simply do $arrErrors['email'] = "The E-mail Field exeeds the limit of <b>50</b> characters"; if you want to do something with the 'toolong' you'll have to construct your array differently Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Author Share Posted March 13, 2007 well basically this script takes care of errors in a registeration form. The problem is multiple errors can occurr under one field and i want to be able to have the fields with errors highlighted with red once an error is found. bare in mind my code is not complete so, some of the functions in my registeration form may not work right now. I am currently more concerned about the error handlign. here is the code: <?php function form(){ $submit = $_GET['submit']; if(isset($submit)){ echo '<form name="hey" action="'.$_SERVER[php_SELF].'"><input type="text" maxlength="12" name="username" value=""><br><input type="text" name="password" value="" maxlength="50"><br><input type="text" maxlength="80" name="email" value=""><br><input type="submit" name="submit" value="submit"></form>'; } } //It Begins. $arrErrors = array(); $conn = mysql_pconnect("localhost","mediacir","entersandman"); mysql_select_db("mediacir_community"); //Get it, get it now. if(isset($_POST['submit'])){ $username = $_GET['username']; $password = $_GET['password']; $email = $_GET['email']; $submit = $_GET['submit']; $ip = GetHostByName($REMOTE_ADDR); $date = date('d m Y @ h:i:s A'); //Empty?! I THINK NOT! if(empty($username)){ $arrErrors['username']= "The Username Field Was Left Blank"; } if(empty($password)){ $arrErrors['password']= "The Password Field Was Left Blank"; } if(empty($email)){ $arrErrors['email']= "The E-mail Field Was Left Blank"; } //Too Long?! I THINK NOT! if(strlen($username) > 12){ $arrErrors['username']= "The Username Field exeeds the limit of <b>12</b> characters"; } if(strlen($password) > 14){ $arrErrors['password']= "The Password Field exeeds the limit of <b>14</b> characters"; } if(strlen($email) > 50){ $arrErrors['email']=>'toolong'= "The E-mail Field exeeds the limit of <b>50</b> characters"; } //Invalid characters...grr... if (preg_match('|[^a-z\d]|i', $username)) { $arrErrors['username'] = "The Username Field contains invalid characters. <B>Alphanumeric (a-z, A-Z, 0-9)</b> are accepted only."; } if (preg_match('|[^a-z\d]|i', $password)) { $arrErrors['password'] = "The Password Field contains invalid characters. <B>Alphanumeric (a-z, A-Z, 0-9)</b> are accepted only."; } //FINALLY! if(!count($arrErrors) > 0){ //Secure the variables $email=strip_tags($email); $username=strip_tags($username); $password=strip_tags($password); $email=addslashes($email); $username=addslashes($username); $password=addslashes($password); //Create the display name $displayname =&$username; $username=strtolower($username); //final filter $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $displayname = mysql_real_escape_string($displayname); $email = mysql_real_escape_string($email); $password = md5($password); echo "done"; }else{ $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>'; // Get each error and add it to the error string // as a list item. foreach ($arrErrors as $error) { $strError .= "<li>$error</li>"; } $strError .= '</ul></div>'; echo "<style> label { width: 80px; text-align: right; float: left; } .formerror { border: 1px solid red; background-color : #FFCCCC; width: auto; padding: 5px 0; } .errortext { padding-left: 80px; font: bold smaller sans-serif; } </style>"; echo $strError; } }else{ echo form(); } ?> Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 what are you trying to do with the 'toolong' part of the array? why is the email addition to the array different than the rest? Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Author Share Posted March 13, 2007 1. i just figured if there are multiple errors for one field in a form, that means multiple keys. 2. the email addition is for the email field in the form. i thought it would make it easier to echo the errors underneath the email field if they are already sorted out in the correct locations. am i correct to do this? Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Author Share Posted March 13, 2007 1. i just figured if there are multiple errors for one field in a form, that means multiple keys. 2. the email addition is for the email field in the form. i thought it would make it easier to echo the errors underneath the email field if they are already sorted out in the correct locations. am i correct to do all this? and how can i fix up my array so it will actually work? right now im getting this: Parse error: syntax error, unexpected T_DOUBLE_ARROW in ***************** on line 47 sorry to double post, but it wont let me modify for some reason:S Quote Link to comment Share on other sites More sharing options...
fert Posted March 13, 2007 Share Posted March 13, 2007 $arrErrors['email']=>'toolong'= "The E-mail Field exeeds the limit of <b>50</b> characters"; so are you trying to change the key "email" to "toolong? Quote Link to comment Share on other sites More sharing options...
emehrkay Posted March 13, 2007 Share Posted March 13, 2007 id say keep your errors as single entries instead of arrays. so if some one puts in an incorrect email address and its over 50 chars long, just make two entries - one as invalid_email the other as invalid_email_length if you were to create the email error as an array arry['email'] = array('length' => 'too long', 'parse' => 'not an email'); when you go to parse the errors, you have to account for this single exception - keep it simple Quote Link to comment Share on other sites More sharing options...
artacus Posted March 13, 2007 Share Posted March 13, 2007 See if this isn't what you want: $arrErrors['email'] = array('type'=>'toolong', 'description'=>"The email ..."); or less likely: $arrErrors['email']['toolong'] = 'The email ...'; Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Author Share Posted March 13, 2007 artacus, if i were to use: <?php $arrErrors['email'] = array('type'=>'toolong', 'description'=>"The email ..."); ?> when echoing the error how would i do it? right now i do: <?php $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>'; // Get each error and add it to the error string // as a list item. foreach ($arrErrors as $error) { $strError .= "<li>$error</li>"; } $strError .= '</ul></div>'; ?> something along the lines of that. Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Author Share Posted March 13, 2007 would this work? <?php $strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>'; // Get each error and add it to the error string // as a list item. foreach ($arrErrors as $type as $description) { $strError .= "<li>$description</li>"; } $strError .= '</ul></div>'; ?> Quote Link to comment Share on other sites More sharing options...
fert Posted March 13, 2007 Share Posted March 13, 2007 foreach ($arrErrors as $key=>$value) Quote Link to comment Share on other sites More sharing options...
HoTDaWg Posted March 13, 2007 Author Share Posted March 13, 2007 thanks:) topic solved. 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.