Jump to content

Conditional sticky form field


kurt.santo

Recommended Posts

I make my form fields sticky, but have problems with resetting the form. I found out that the reset button always reverts back to the initial stage of a form, in case of sticky form fields the entries given. Is there a way to amend:

value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"

so it only stays sticky if that particular field was entered correctly? If there is an entry in the error array ($errors['name'] = 'This is a required field':) it won't? Not sure about syntax of such an if clause...Kurt

Link to comment
Share on other sites

Instead of echoing $_POST['name'] for the value, set variables for each input value. Start each variable off as an empty string. If they get it right, then write the $_POST value to the variable. If they get it wrong don't write anything. By the time you write the variables in as the values for the inputs, the ones with problems won't have anything in them to write.

Link to comment
Share on other sites

Instead of echoing $_POST['name'] for the value, set variables for each input value. Start each variable off as an empty string. If they get it right, then write the $_POST value to the variable. If they get it wrong don't write anything. By the time you write the variables in as the values for the inputs, the ones with problems won't have anything in them to write.
Would I do this in the same place as the code I gave? Guess this is getting a bit too long. Also, how do I make the comparision to check if they got it right or wrong? Is is like if $errors['name'] == "" or similar?Kurt
Link to comment
Share on other sites

No, the code you gave would be replaced with something like this:

value="<?php echo $name; ?>"

At the top of the script you initialize the variable:$name = '';As you're going through the script validating the form, if the field passes validation then you copy the value from $_POST into the variable. That way if they got it right then the variable will hold the value and will be printed when it gets to the form. If the field did not pass validation then you don't copy any value into it and it stays as the empty string so when it gets written out later it won't write out the incorrect value.

Also, how do I make the comparision to check if they got it right or wrong?
I assumed you were already validating the form somewhere.
Link to comment
Share on other sites

No, the code you gave would be replaced with something like this:
function check_error ($field, $text) {global $errors;$name = '';if (empty($errors[$field])) {	echo $text;$name = ($_POST['field']);	} else {	echo $text;	echo ' ';	echo '<strong>';	echo $errors[$field];	echo '</strong>';	}}

Is this what you mean? As usual I am just a bit confused as there are several fields...Kurt

Link to comment
Share on other sites

That's not the function to change, that function writes the error message. You need to find where you validate the form in the first place, where you set the $errors array to have an error message or not.
Will try that when am back at work tomorrow. Thanks, Kurt
Link to comment
Share on other sites

Had a go, but does not work. I tried inserting a $fn_value after:

	$fn_value ='';	// check for a first name	if (!isset($_POST['first_name']) OR empty($_POST['first_name'])) {		$first_name = FALSE;		$errors['first_name'] = '\'First Name\' is a required field';		} else {		if (eregi ('^[[:alpha:]\.\' \-]{2,30}$', stripslashes(trim($_POST['first_name'])))) {			$first_name = escape_data($_POST['first_name']);			$fn_value = $first_name;			} else {			$first_name = FALSE;			$errors['first_name'] = 'Your input for \'First Name\' seems to be incorrect';			}		}

with:

<input type="text" name="first_name" size="30" maxlength="30" value="<?php echo $fn_value; ?>" />

It says "undefined variable" and I found that when error reporting is set to the highest level of error reporting this is quite normal (which should stay this way). I also wondered if there is a way to utilise the already existing $first_name variable and kind of have a test to write $_POST['first_name'] only if $first_name is not false. Would that be doable?Kurt

Link to comment
Share on other sites

It shouldn't say undefined if you are defining the variable on top. Make sure the definition is not inside any if statements or anything like that. It should always get set regardless of anything else happening on the page. All variable declarations should go on the top if you're counting on them being defined later in the page.

Link to comment
Share on other sites

It shouldn't say undefined if you are defining the variable on top. Make sure the definition is not inside any if statements or anything like that. It should always get set regardless of anything else happening on the page. All variable declarations should go on the top if you're counting on them being defined later in the page.
That was the problem, I had it inside an if clause... Cheers.Can that also be achieved with select drop-down fields? Then it would be perfect!Kurt (slowly getting there...)
Link to comment
Share on other sites

I can't see a reason why you can't do that for anything else. The only thing different about select fields is needing to compare the value of each option with the variable to figure out which one to write selected="selected" on.

Link to comment
Share on other sites

I can't see a reason why you can't do that for anything else. The only thing different about select fields is needing to compare the value of each option with the variable to figure out which one to write selected="selected" on.
Will have a go tomorrow (call it a day;-)). Pleased the rest is working fine, cheers!Kurt
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...