AdRock Posted August 24, 2007 Share Posted August 24, 2007 I have some form validation and I want to put all the error messages that are displayed into a bulleted list. I have seen it done with more complicated validation using OOP but I don't know enough to do that. How would I add these error messages to a list if they occur? if(empty($first_name)) { $error['first_name'] = true; $print_again = true; $message="The forename field is empty<br />"; } else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) { $error['first_name'] = true; $print_again = true; $message="Forename must contain letters only<br />"; } if(empty($last_name)) { $error['last_name'] = true; $print_again = true; $message.="The surname field is empty<br />"; } else if(!ereg("^[A-Za-z\-]{2,30}$",$last_name)) { $error['last_name'] = true; $print_again = true; $message.="Surname must contain letters only<br />"; } if(empty($email)) { $error['email'] = true; $print_again = true; $message.="The email field is empty<br />"; } if( mysql_num_rows(mysql_query("SELECT email_address FROM users WHERE email_address = '$email'")) ) { $error['email'] = true; $print_again = true; $message.="Your email address has already been used by another member in our database. Please submit a different Email address!!<br />"; } else if(!check_email_address($email)) { $error['email'] = true; $print_again = true; $message.="Email address in invalid format<br />"; } if(empty($username)) { $error['username'] = true; $print_again = true; $message.="The username field is empty<br />"; } else if( mysql_num_rows(mysql_query("SELECT username FROM users WHERE username = '$username'")) ) { $error['username'] = true; $print_again = true; $message.="The username you have selected has already been used by another member in our database. Please choose a different Username!<br />"; } else if(!ereg("^[A-Za-z0-9\-]{3,30}$",$username)) { $error['username'] = true; $print_again = true; $message.="Username must contain letters and numbers only<br />"; } if(empty($password1)) { $error['password1'] = true; $print_again = true; $message.="The password field is empty<br />"; } else if(!ereg("^[A-Za-z0-9]{6,30}$",$password1)) { $error['password1'] = true; $print_again = true; $message.="Password must contain letters and numbers only<br />"; } if (strcmp( $password1,$password2 ) !=0){ $error['password1'] = true; $error['password2'] = true; $print_again = true; $message.="The passwords didn't match<br />"; } if (empty($_POST['verify']) && $_POST['verify'] == $_SESSION['captchstr']) { $error['verify'] = true; $error['password1'] = true; $error['password2'] = true; $print_again = true; $message.="Please enter Anti-Spam key<br />"; } I use this bit to redisplay the form and display the error messages if($print_again) { echo "<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p>$message"; show_form(); } else { Link to comment https://forums.phpfreaks.com/topic/66564-solved-putting-form-error-messages-into-a-list/ Share on other sites More sharing options...
pocobueno1388 Posted August 24, 2007 Share Posted August 24, 2007 I did an example with the first two possible error outputs for you: <?php if(empty($first_name)) { $error['first_name'] = true; $print_again = true; $message .="<li>The forename field is empty</li>"; } else if(!ereg("^[A-Za-z]{2,30}$",$first_name)) { $error['first_name'] = true; $print_again = true; $message .="<li>Forename must contain letters only</li>"; } ?> And with the snippet of code that displays the errors, just do this: <?php if($print_again) { echo "<h2 class=\"errorhead\">There has been an error:</h2><p>You forgot to enter the following field(s)</p> <ul>$message</ul>"; show_form(); } else { ?> Link to comment https://forums.phpfreaks.com/topic/66564-solved-putting-form-error-messages-into-a-list/#findComment-333403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.