strackan Posted March 26, 2006 Share Posted March 26, 2006 Hello,I am relatively new to php/mysql and have been trying to get the following code to work, without success for about a week. Please help a poor newbie.Here is my initial code:[code]<?php// checkBlank.php -- a program to check for blank entries in a form?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Empty Fields</title></head><body><?php// set up array of label fields$label_array = array ( "first_name" => "First Name", "middle_name" => "Middle Name", "last_name" => "Last Name", "phone" => "Phone" );// check each field for blanksforeach ($_POST as $field => $value){ if ($field != "middle_name") { if ( !$value ) { $blank_array[$field] = "blank"; } }}// if any fields are blank, display error message and formif (@sizeof($blank_array) > 0) // if blank fields are found{ echo "<b>You didn't fill in one or more required fields. You must enter:</b>";// display list of missing information foreach ($blank_array as $field => $value) { echo "<b><blockquote>$label_array[$field]</blockquote></b>"; } // end foreach loop// redisplay form$first_name=trim(strip_tags($_POST['first_name']));$middle_name=trim(strip_tags($_POST['middle_name']));$last_name=trim(strip_tags($_POST['last_name']));$phone=trim(strip_tags($_POST['phone']));echo "<p><hr> <form action='checkBlank.php' method='POST'> <center> <table width='95%' border='0' cellspacing='0' cellpadding='2'> <tr><td align='right'><b>{$label_array['first_name']}: </b></td> <td><input type='text' name='first_name' size='65' maxlength='65' value='$first_name' ></td> </tr> <tr><td align='right'><b>{$label_array['middle_name']}: </b></td> <td><input type='text' name='middle_name' size='65' maxlength='65' value='$middle_name' ></td> </tr> <tr><td align='right'><b>{$label_array['last_name']}: </b></td> <td><input type='text' name='last_name' size='65' maxlength='65' value='$last_name' ></td> </tr> <tr><td align='right'><b>{$label_array['phone']}: </b></td> <td><input type='text' name='phone' size='65' maxlength='65' value='$phone' ></td> </tr> </table> <p><input type='submit' value='Submit name and phone number' > </form> </center>";exit();}echo "Welcome to your new account. You may now <a href='login.php'>log in</a>.";?></body></html>[/code]When I view this locally, I see:Welcome to your new account. Please log in.The form is completely bypassed. So, I tried moving the '}' after the exit(); (line 68) line to the line right before the \\redisplay form comments (line 37). When I do that, I get the form, and it works flawlessly, except when I finally input all the required fields, the form just continues to redisplay. It does not display the welcome message.In short, I can't get the form to display the welcome message upon successfully inputting all required fields. I'm guessing this is due to a missing ELSE statement or bracket, but I can't for the life of me figure out what I'm missing. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/5870-php-required-forms-help/ Share on other sites More sharing options...
js_280 Posted March 27, 2006 Share Posted March 27, 2006 How about using this workaround...[code]<?php// checkBlank.php -- a program to check for blank entries in a form?><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Empty Fields</title></head><body><?php// set up array of label fields$label_array = array ( "first_name" => "First Name", "middle_name" => "Middle Name", "last_name" => "Last Name", "phone" => "Phone" );// check each field for blanks@$first_name = trim(strip_tags($_POST['first_name']));@$middle_name = trim(strip_tags($_POST['middle_name']));@$last_name = trim(strip_tags($_POST['last_name']));@$phone = trim(strip_tags($_POST['phone']));if(strlen($first_name) < 1) { $blank_array['first_name'] = 'blank'; }if(strlen($last_name) < 1) { $blank_array['last_name'] = 'blank'; }if(strlen($phone) < 7) { $blank_array['phone'] = 'blank'; }// if any fields are blank, display error message and formif (@count($blank_array) > 0) // if blank fields are found{ echo "<b>You didn't fill in one or more required fields. You must enter:</b>";// display list of missing information foreach ($blank_array as $field => $value) { echo "<b><blockquote>$label_array[$field]</blockquote></b>"; } // end foreach loop// redisplay formecho("<p><hr><form action='checkBlank.php' method='POST'><center><table width='95%' border='0' cellspacing='0' cellpadding='2'><tr><td align='right'><b>");echo($label_array['first_name']);echo(":</b></td><td><input type='text' name='first_name' size='65' maxlength='65' value='$first_name' ></td></tr><tr><td align='right'><b>{$label_array['middle_name']}:</b></td><td><input type='text' name='middle_name' size='65 'maxlength='65' value='$middle_name' ></td></tr><tr><td align='right'><b>{$label_array['last_name']}:</b></td><td><input type='text' name='last_name' size='65' maxlength='65' value='$last_name' ></td></tr><tr><td align='right'><b>{$label_array['phone']}:</b></td><td><input type='text' name='phone' size='65' maxlength='65' value='$phone' ></td></tr></table><p><input type='submit' value='Submit name and phone number' ></form></center>");exit;}echo "Welcome to your new account. You may now <a href='login.php'>log in</a>.";?></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5870-php-required-forms-help/#findComment-21104 Share on other sites More sharing options...
strackan Posted March 28, 2006 Author Share Posted March 28, 2006 First off, thanks a lot for your help. I think I'm a lot closer now. I just tried this code, and it seems to work fine, except for one problem:When the form is first displayed, it gives the blank fields error:[i]You didn't fill in one or more required fields. You must enter: First Name Last Name Phone[/i]After that it works fine. How can I ensure that the form understands not to display blank field notices when it is first loaded?Thanks again... Quote Link to comment https://forums.phpfreaks.com/topic/5870-php-required-forms-help/#findComment-21453 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.