doubledee Posted June 30, 2012 Share Posted June 30, 2012 Currently my Forms are "sticky" if a User enters valid data. However, if the enter invalid data, I erase the field - more like I don't keep the value - and display an error message. Here is a sample of my coding style... // Validate First Name. (Required) if (empty($trimmed['firstName'])){ // No First Name. $errors['firstName'] = 'Enter your First Name.'; }else{ // First Name Exists. if (preg_match('#^[A-Z \'.-]{2,30}$#i', $trimmed['firstName'])){ // Valid First Name. $firstName = $trimmed['firstName']; }else{ // Invalid First Name. $errors['firstName'] = 'First Name must be 2-30 characters (A-Z \' . -)'; } }//End of VALIDATE FIRST NAME And then in my Form I have... <!-- First Name --> <label for="firstName"><b>*</b>First Name:</label> <input id="firstName" name="firstName" type="text" maxlength="30" value="<?php if(isset($firstName)){echo htmlentities($firstName, ENT_QUOTES);} ?>" /><!-- Sticky Field --> <?php if (!empty($errors['firstName'])){ echo '<span class="error">' . $errors['firstName'] . '</span>'; } ?> It seems to me that people were criticizing this approach in the past? If this provides a bad UX, then how should I modify my code so that the User doesn't have to re-type in their entire response? (Maybe they just accidentally had a type-o...) Follow me? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265018-question-about-sticky-form/ Share on other sites More sharing options...
doubledee Posted June 30, 2012 Author Share Posted June 30, 2012 Does this look better??? // Validate First Name. (Required) $firstName = $trimmed['firstName']; if (empty($firstName)){ // No First Name. $errors['firstName'] = 'Enter your First Name.'; }else{ // First Name Exists. if (preg_match('#^[A-Z \'.-]{2,30}$#i', $firstName)){ // Valid First Name. // Continue processing... }else{ // Invalid First Name. $errors['firstName'] = 'First Name must be 2-30 characters (A-Z \' . -)'; } }//End of VALIDATE FIRST NAME Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265018-question-about-sticky-form/#findComment-1358061 Share on other sites More sharing options...
kicken Posted June 30, 2012 Share Posted June 30, 2012 The only fields you generally don't re-populate are sensitive information fields such as a password, cc number, ssn, etc. Most of the time if the field is invalid it is going to be just a small typo the user made, such as missing the . in their email or something. It's somewhat annoying to have to re-type it all when all you really need to do is made a small adjustment. Quote Link to comment https://forums.phpfreaks.com/topic/265018-question-about-sticky-form/#findComment-1358139 Share on other sites More sharing options...
doubledee Posted June 30, 2012 Author Share Posted June 30, 2012 The only fields you generally don't re-populate are sensitive information fields such as a password, cc number, ssn, etc. Most of the time if the field is invalid it is going to be just a small typo the user made, such as missing the . in their email or something. It's somewhat annoying to have to re-type it all when all you really need to do is made a small adjustment. I get that part now. But the intent of this thread was see if my re-written code looks okay and does what we are talking about. From my testing, it seems to do what it should and I think it is efficient. So, again, how does this look... // Validate First Name. (Required) $firstName = $trimmed['firstName']; // $firstName = isset($trimmed['firstName']) ? $trimmed['firstName'] : ''; if (empty($firstName)){ // No First Name. $errors['firstName'] = 'Enter your First Name.'; }else{ // First Name Exists. if (preg_match('#^[A-Z \'.-]{2,30}$#i', $firstName)){ // Valid First Name. // Continue processing... }else{ // Invalid First Name. $errors['firstName'] = 'First Name must be 2-30 characters (A-Z \' . -)'; } }//End of VALIDATE FIRST NAME By defining $firstName before I get into my IF-THEN-ELSE's, my Form always has a value, whether it is right or wrong. The only part I'm not sure of is the first line?! On one hand, it appears that Input and textArea fields always return an Empty String is they are blank. If that is 100% true, then my first line should be good enough. But if there is a way for a hacker to break this rule, then I think I need the commented out line instead. Would it be safer to use this to start things off... $firstName = isset($trimmed['firstName']) ? $trimmed['firstName'] : ''; Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/265018-question-about-sticky-form/#findComment-1358148 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.