Hello, I am developing a website for which I am designing a signup form, the problem is a user fills the form and miss a field when they go back to enter that field it cleans up all the remainig field and they have to fill the whole form again. Please help me how can I achieve this.

Their are various way to get this done...

1) User java script to validate the form .. this is for client side validation
2) use Ajax to validate the form and tell user what he has missed on that form this is for server side validation
3) Use sessions to preserve the values
4) Use the Php code on top of the form and from that form call same php page if you find any issues you can show him on that page will form filled with what ever he had filled.

these are just basic ways their are more let me know if i can help you more

The suggestions einfoway made are correct, however there are other factors to take into account, for example some people do not have JavaScript enabled so the Javascript validation and AJAX will not work for this so it would not be a good idea to rely on this.

It would be a good idea to employ both JavaScript client-side validation on the form as well as some sort of PHP validation with sessions to remember the values in the fields. That way if the JavaScript validation is bypassed the PHP should pick up any errors.

Thanks guys but problem is I want to do this for a form having 77 fields. Isn't there a simple method.

If the validation is as simple as 'is the field empty' then it would be fairly simple to get JavaScript to validate them with a single function, for example,
JavaScript code:

<language="text/javascript">
function CheckField(field) {
    if (field.value == "") {
    alert( "Field " + field.name + " is empty" );
    form.field.focus();
    return false ;
  }
  return true ;
}
</script>

Then add the following to each field which need validating:

onsubmit="return checkform(this);"

However, this assumes that they will click in every field, it will not work if the field is not clicked in.


If the validation is more in-depth than this it would take a lot more.
The same applies to PHP, there is not really a 'simple solution'

Thanks guys but problem is I want to do this for a form having 77 fields. Isn't there a simple method.

77 fields is large number i think javascript is a must for client side validation and for server side use sessions (PHP) It should not take that much time to have sessions implemented on all 77 fields. Yes if it has dropdowns and check boxes then you will need to write small logic.

Yes.

On the file that is processing the request you want to make sure that you have session_start(); at the top of the file, and in the case that the form inputs are invalid or blank you use the following line of code:

<?
$_SESSION['formfields'] = array();
foreach($_POST as $key=>$value)
{
	$_SESSION[$key] = $value;
	$_SESSION['formfields'][] = $key;
}
$_SESSION['formfields'] = NULL;
unset($_SESSION['formfields']);
?>

Then on your form for text fields including text boxes and textareas you consult the following example

//text box
<input type="text" name="fname" id="fname" value="<? echo isset($_SESSION['fname'])?$_SESSION['fname']:$originalvalue; ?>" />

//text area
<textarea name="description" id="description"><? echo isset($_SESSION['fname'])?$_SESSION['fname']:$originalvalue; ?></textarea>

for select drop downs

<select name="selState" id="selState">
	<option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "AZ"?' selected="selected"':""; ?> value="AZ">AZ</option>
    <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "CO"?' selected="selected"':""; ?> value="CO">CO</option>
    <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "AK"?' selected="selected"':""; ?> value="AK">AK</option>
    <option<? echo isset($_SESSION['selState']) && $_SESSION['selState'] == "ME"?' selected="selected"':""; ?> value="ME">ME</option>
</select>

for multi-select drop downs

<select name="selState[]" id="selState">
	<option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option>
    <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option>
    <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option>
    <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option>
</select>

for radio buttons and check boxes

//check boxes
<input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("red", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsred" value="red" />&nbsp;<label for="chkColorsred">Red</label><br />
<input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("green", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsgreen" value="green" />&nbsp;<label for="chkColorsgreen">Green</label><br />
<input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("blue", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorsblue" value="blue" />&nbsp;<label for="chkColorsblue">Blue</label><br />
<input type="checkbox"<? echo isset($_SESSION['chkColors']) && in_array("purple", $_SESSION['chkColors'])?' checked="checked"':""; ?> name="chkColors[]" id="chkColorspurple" value="purple" />&nbsp;<label for="chkColorspurple">Purple</label><br />

//radio buttons
<input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "red"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsred" value="red" />&nbsp;<label for="rdoColorsred">Red</label><br />
<input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "green"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsgreen" value="green" />&nbsp;<label for="rdoColorsgreen">Green</label><br />
<input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "blue"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorsblue" value="blue" />&nbsp;<label for="rdoColorsblue">Blue</label><br />
<input type="radio"<? echo isset($_SESSION['rdoColors']) && $_SESSION['rdoColors'] == "purple"?' checked="checked"':""; ?> name="rdoColors" id="rdoColorspurple" value="purple" />&nbsp;<label for="rdoColorspurple">Purple</label><br />

On the top of your form you also want to put

<? session_start(); ?>

When ever you want to clear the form fields from your session variables just run this code.

<?
foreach($_SESSION['formfields'] as $value)
{
	$_SESSION[$value] = NULL;
	unset($_SESSION[$value]);
}
$_SESSION['formfields'] = NULL;
unset($_SESSION['formfields']);
?>

That should cover just about everything, let me know if something doesn't work quite the way it should.

1 correction, this first block:

<?
$_SESSION['formfields'] = array();
foreach($_POST as $key=>$value)
{
	$_SESSION[$key] = $value;
	$_SESSION['formfields'][] = $key;
}
$_SESSION['formfields'] = NULL;
unset($_SESSION['formfields']);
?>

is suppose to be:

<?
$_SESSION['formfields'] = array();
foreach($_POST as $key=>$value)
{
	$_SESSION[$key] = $value;
	$_SESSION['formfields'][] = $key;
}
?>

for multi-select drop downs

<select name="selState[]" id="selState">
	<option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option>
    <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option>
    <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option>
    <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option>
</select>

forgot the multiple="multiple" in the select tag too

<select multiple="multiple" name="selState[]" id="selState">
	<option<? echo isset($_SESSION['selState']) && in_array("AZ", $_SESSION['selState'])?' selected="selected"':""; ?> value="AZ">AZ</option>
    <option<? echo isset($_SESSION['selState']) && in_array("CO", $_SESSION['selState'])?' selected="selected"':""; ?> value="CO">CO</option>
    <option<? echo isset($_SESSION['selState']) && in_array("AK", $_SESSION['selState'])?' selected="selected"':""; ?> value="AK">AK</option>
    <option<? echo isset($_SESSION['selState']) && in_array("ME", $_SESSION['selState'])?' selected="selected"':""; ?> value="ME">ME</option>
</select>

Wow I am posting a lot in this thread.

You can make it easier by condensing it down into a function or class.

<?
function repopulateData($inputtype, $inputname, $testvalue, $originalvalue = "")
{
	switch($inputtype)
	{
		case "radio":
			echo isset($_SESSION[$inputname]) && $_SESSION[$inputname] == $testvalue?' checked="checked"':"";
			break;
		case "checkbox":
			echo isset($_SESSION[$inputname]) && in_array($testvalue, $_SESSION[$inputname])?' checked="checked"':"";
			break;
		case "multi-select":
			echo isset($_SESSION[$inputname]) && in_array($testvalue, $_SESSION[$inputname])?' selected="selected"':"";
			break;
		case "select":
			echo isset($_SESSION[$inputname]) && $_SESSION[$inputname] == $testvalue?' selected="selected"':"";
			break;
		case "text":
			echo isset($_SESSION[$inputname])?$_SESSION['fname']:$originalvalue;
			break;
	}
}
?>

then just call it like:

repopulateData("checkbox", "fieldname", "checkbox value", ""/*for textareas and text boxes*/);
This article has been dead for over six months. Start a new discussion instead.