Jump to content

Question about Sticky Form


doubledee

Recommended Posts

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?  :shrug:

 

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

 

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?!  :confused:

 

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

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.