doubledee Posted March 31, 2011 Share Posted March 31, 2011 I am getting an "undefined" error at these two lines... <?php echo $errors['firstName']; ?> <?php echo $errors['lastName']; ?> Here is my file... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link type="text/css" rel="stylesheet" href=".css"> <style type="text/css" > form{ width: 400px; margin: 0 auto; } </style> </head> <body> <?php // Initialize. $errors = array(); $firstName = $lastName = ''; if (isset($_POST['submitted'])){ // Handle Form. // Trim all incoming data. $trimmed = array_map('trim', $_POST); // Check First Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['firstName'])){ $firstName = $_POST['firstNamae']; }else{ $errors['firstName'] = 'Please enter your First Name.'; } // Check Last Name. if (preg_match('/^[A-Z\'.-]{2,20}$/i', $_POST['lastName'])){ $lastName = $_POST['lastName']; }else{ $errors['lastName'] = 'Please enter your Last Name.'; } // if there are errors then go back to the form and display them }else{ } ?> <form action=""> <fieldset> <legend>Billing Details</legend> <ol> <li> <label for="firstName">First Name:</label> <input id="firstName" name="firstName" class="text" type="text" /> <?php echo $errors['firstName']; ?> </li> <li> <label for="lastName">Last Name:</label> <input id="lastName" name="lastName" class="text" type="text" /> <?php echo $errors['lastName']; ?> </li> </ol> <input class="submit" type="submit" value="Process Order" /> </fieldset> </form> </body> </html> I thought things were initialized okay?! Sincerely, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232341-need-help-initializing-array/ Share on other sites More sharing options...
AbraCadaver Posted March 31, 2011 Share Posted March 31, 2011 You only assign a value to those array elements when $_POST['submitted'] is set. On your first load of the form those elements don't exists. Quote Link to comment https://forums.phpfreaks.com/topic/232341-need-help-initializing-array/#findComment-1195249 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 You only assign a value to those array elements when $_POST['submitted'] is set. On your first load of the form those elements don't exists. But I initialized my array outside of the the If-then-Else?! <?php // Initialize. $errors = array(); $firstName = $lastName = ''; if (isset($_POST['submitted'])){ // Handle Form. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232341-need-help-initializing-array/#findComment-1195258 Share on other sites More sharing options...
AbraCadaver Posted March 31, 2011 Share Posted March 31, 2011 Yes, so you have an empty array called $errors but there is no $errors['firstName'] or $errors['lastName']. That's why it's complaining. Quote Link to comment https://forums.phpfreaks.com/topic/232341-need-help-initializing-array/#findComment-1195265 Share on other sites More sharing options...
doubledee Posted March 31, 2011 Author Share Posted March 31, 2011 Yes, so you have an empty array called $errors but there is no $errors['firstName'] or $errors['lastName']. That's why it's complaining. I have always found it a real pain in the ____ that on one hand PHP doesn't want you defining datatypes and initializing variables and then it complains about stuff like this?! So is there an easy way to initialize my array and make the errors go away? (On my actual form there could 30+ fields. I cannot believe I have to initialize each variable key value?!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/232341-need-help-initializing-array/#findComment-1195270 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.