Jim R Posted September 1, 2011 Share Posted September 1, 2011 If I wanted to make multiple fields required before letting a User move past the field is this the proper way to do it? if(isSet($_POST['playerFirst']['playerLast']['feet']['inches']['year']['status'])) { ## Something } else { ## Something else } Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/ Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 That must be one HUGE multi-dem array. Also, you should be using empty() for this, as a variable could be set, but not have any value. Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264134 Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2011 Share Posted September 1, 2011 No, not at all actually. What you have there would be a single form field in a multidimensional $_POST array. The proper way to do it would be to validate each field that is required, and store any validation errors in an array. If the array is empty after the validation process, then there are no errors, and whatever processing such as DB insert, etc. can be done. If the array is not empty, there are errors, so display them to the user along with the form pre-populated with the values they submitted so then can correct the fields that need it and resubmit the form. Look at this code, paste it in and run it. Play with it and see if it makes sense at all to you. <?php if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted $errors = array(); // initialize an array to hold validation errors $_POST = array_map('trim', $_POST); // trim all $_POST array values if( !empty($_POST['name']) ) { // validate the name field if( !ctype_alpha($_POST['name']) ) { $errors['name'][] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error } if( strlen($_POST['name']) < 3 || strlen($_POST['name'] > 20) ) { $errors['name'][] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error } } else { $errors['name'][] = 'Name is a required field.'; // if name is empty, store error } if( !empty($_POST['number']) ) { // same validations as in name, above. if( !ctype_digit($_POST['number']) ) { $errors['number'][] = 'Number must be numeric.'; } if( strlen($_POST['number']) < 5 || strlen($_POST['number']) > 10 ) { $error = 'Number must be from 3 to 20 digits. It is currently ' . strlen($_POST['number']) . ' digit'; $error .= strlen($_POST['number']) == 1 ? '.' : 's.'; $errors['number'][] = $error; } } else { $errors['number'][] = 'Number is a required field.'; } if( !empty($errors) ) { // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form $echo = array(); foreach( $errors as $v ) { if( is_array($v) ) { $echo[] = implode('<br>', $v ); } else { $echo[] = $v; } } $err_echo ="<font color=\"red\">The following errors were detected:<br>"; $err_echo .= implode("<br>\n", $echo); $err_echo .= '</font>'; } } if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > <style type="text/css" media="screen"> body { font-family: helvetica, arial, sans-serif; font-size: 0.85em; line-height: 1.25em; letter-spacing: -0.5px; } input { border: 1px solid #336699; padding: 0.1em; margin: 5px; color: #113366; } input.error { background-color: #F2BDCA; color: #850310; border: 1px solid red; } input.good { background-color: #D3F5D3; border: 1px solid #156B15; color: #156B15; } input.submit { background-color: #CCCCCC; border: 1px solid #888888; color: #333333; padding: 2px; margin: 0; font: 0.9em helvetica, arial sans-serif; } </style> <title> Work In Progress</title> </head> <body> <?php echo !empty($err_echo) ? $err_echo : ''; ?> <form method="post" action=""> Name (3-20 letters): <input type="text" class="<?php if( isset($_POST['submitted']) ) { echo !empty($errors['name']) ? 'error' : 'good'; } ?>" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>"> <br> Number (5-10 numbers): <input type="text" class="<?php if( isset($_POST['submitted']) ) { echo !empty($errors['number']) ? 'error' : 'good'; } ?>" name="number" value="<?php echo isset($_POST['number']) ? $_POST['number'] : ''; ?>"> <br> <input type="hidden" name="submitted" value="yes"> <input class="submit" type="submit" name="submit" value=" <?php echo !empty($errors) ? 'Re-Submit' : 'Submit'; ?> "> </form> <?php } else { // Form was submitted, and validated with no errors. OK to run db insert, display success message, etc. echo "Successful submission!"; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264136 Share on other sites More sharing options...
Jim R Posted September 1, 2011 Author Share Posted September 1, 2011 Wait...maybe I screwed something up. I just want to make sure Users fill out those fields before moving on. Most would just fill in the name and try to move on. I have a Live Update set up, so it's showing submissions in real time. I'm just trying to make some fields required. Pikachu, what you have seems to be just a bit more complicated than I really need. Can I check for required fields in the way I posted above? Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264141 Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2011 Share Posted September 1, 2011 Not unless you have a field in your form with: <input type="text" name="playerFirst[playerLast][feet][inches][year][status]"> . . . You can't just tack on all of the array indices to $_POST like that. You should also use empty() instead of isset() and trim() the values to prevent submission of nothing but a bunch of whitespace. Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264142 Share on other sites More sharing options...
Jim R Posted September 1, 2011 Author Share Posted September 1, 2011 "empty" I get. Would I use if...elseif...elseif...etc to cover each entry? Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264143 Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2011 Share Posted September 1, 2011 You'd really be best off to do it like in the code I posted. You can strip out the CSS field highlighting and other things if they aren't needed, but the general idea is the same. You want to validate all fields at once, and display all the errors to the user at the same time so they can edit once and move on instead of submitting the form, correcting an error, submitting again and correcting another error, etc. What are you referring to with "live update", BTW? Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264148 Share on other sites More sharing options...
Jim R Posted September 1, 2011 Author Share Posted September 1, 2011 I guess I'm not grasping what you're saying. All I care about is someone not trying to forward an empty field. I'm dealing with a specific User base, coaches. They aren't going to be the type that tries to get cute with entries, and I won't have to worry about checking for specific characters. They will all type in the names, but once they get far enough down their roster, they might try to cut some corners by not submitting height, grade, etc. If they leave a required field blank, I'd have it say to make sure all required fields are filled. If all are filled, the form will make see if the name is already entered. If so, it will treat the submission as an update. If not it will insert a new row of data. I have all of that done except checking for required fields. Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264157 Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 Good programmers always cover all of the bases (pun intended) whether they have a specific base of user they expect or not. Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264344 Share on other sites More sharing options...
Jim R Posted September 1, 2011 Author Share Posted September 1, 2011 I get that, but I have a specific user base, all professionals in the same field, which I know them all by name. In a 12 field form, I just need to make sure they answer those in the least. Four of them are drop downs. Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264346 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2011 Share Posted September 1, 2011 And what is the current problem with just doing this? Pika already told you - You can strip out the CSS field highlighting and other things if they aren't needed, but the general idea is the same. Doing that (stripping out the other things) to one representative example from the code he posted, produces this logic - <?php if( !empty($_POST['name']) ) { // validate the name field // code for the other things was here... } else { $errors['name'][] = 'Name is a required field.'; // if name is empty, store error } Rearranging and simplifying the logic - <?php if( empty($_POST['name']) ) { // validate the name field $errors['name'][] = 'Name is a required field.'; // if name is empty, store error } Done. Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264348 Share on other sites More sharing options...
Jim R Posted September 1, 2011 Author Share Posted September 1, 2011 That was my next question above. Basically I just use If statements for each required field? Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1264350 Share on other sites More sharing options...
eazyGen Posted September 5, 2011 Share Posted September 5, 2011 Good programmers always cover all of the bases (pun intended) whether they have a specific base of user they expect or not. I would say that this is the single most important statement in the whole topic. To base your validation on an expected type of user could be troublesome I would say. What would happen if the user type changed? Maybe they start employing low paid clerks? Then you could in in trouble. The app would "break" because a different person is sat in front of it S Quote Link to comment https://forums.phpfreaks.com/topic/246151-question-about-isset-making-form-fields-required/#findComment-1265522 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.