Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error checking form input 1

Status
Not open for further replies.

mickallen

Technical User
Jan 5, 2001
39
GB
How can I keep the data already entered into a form after error checking to make sure the user has filled in all form fields correctly.
I want to display an error message and direct them back to the form to correct any errors.
I only seem to be able to bring back a blank form after displaying the error.
 
1. In your error page, include <input type=hidden> tags with the keys and values for all the fields in your main form. Submit back to the main form page.

2. When the form reloads, you can put something like this in your value= attributes:

Code:
value='<?php echo $var; ?>'

::OR::

2. I tend to make forms that utilize all the different input types, and that's a pain in the ass using the above method. So instead, I use javascript to interpret the form data and insert the values into the appropriate input fields/checkboxes/selects/etc. Here's an excerpt:

Code:
<script language='Javascript1.2'>
// Define values
<?php
while(list($var,$value) = each($HTTP_POST_VARS)) {
    if($value == 'on') {
        $checks[] = $var;
        continue;
    }
    echo &quot;var $var = '$value';\n&quot;;
}
if(sizeof($checks) > 0) {
    echo &quot;\nvar checks = new Array('&quot;.implode(&quot;','&quot;,$checks).&quot;');\n&quot;;
}
?>
function importData() {
  // <input type='text'> field names go in here
  inputFields = new Array();
  // <select> field names go in here
  selectFields = new Array();

  for(i = 0; i < inputFields.length; i++) {
    if(eval(inputFields[i]) != null);
    setInputField(inputFields[i],eval(inputFields[i]));
  }

  for(i = 0; i < selectFields.length; i++) {
    setSelectField(selectFields[i],eval(selectFields[i]));
  }

  for(i = 0; i < checks.length; i++) {
    setCheckbox(checks[i]);
  }
}

function setInputField(name,value) {
  object = eval(&quot;document.forms[0].&quot;+name);
  object.value = value;
}

function setSelectField(name,value) {
  object = eval(&quot;document.forms[0].&quot;+name);
  exitWhenSet: for(i = 0; i < object.options.length; i++) {
    if(object.options[i].value == value) {
      object.options[i].selected = true;
      break exitWhenSet;
    }
  }
}

setRadioButton(name,value) {
  object = eval(&quot;document.forms[0].&quot;+name);
  exitWhenSet: for(i = 0; i < object.length; i++) {
    if(object[i].value == value) {
      object[i].checked = true;
      break exitWhenSet;
    }
  }
}

function setCheckbox(name) {
  object = eval(&quot;document.forms[0].&quot;+name);
  object.checked = true;
}
</script>

And then at the very end of the page:

Code:
<script language='Javascript1.2'>
document.onLoad = importData();
</script>

Please keep in mind, I'm not a Javascript programmer, so this is probably pretty rudimentary. But it works well enough, and is an extremely flexible binding between client- and server-side scripting.

Hope this helps,

brendanc@icehouse.net

 
sophisticate

Thanks for the help I have managed to solve the problem from the advice you gave.

Thanks
Mick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top