Forum Moderators: coopster

Message Too Old, No Replies

Form resets after reload

         

sakredei

4:22 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



i just set up a validation on this form but when there is an error and the user hits back to fix it, all the fields are reset. Is there a code to fix this?

mikesmith76

7:04 pm on Dec 8, 2006 (gmt 0)

10+ Year Member



I'd simply redisplay the form on error with any valid data in place from the last submission. no need to click the back button then

justgowithit

12:06 am on Dec 9, 2006 (gmt 0)

10+ Year Member



I'll elaborate on mikesmith76's post. Wherever you post your form (the same page or another) you should have a copy of that form to redisplay incase of errors. Using isset() makes it simple to redisplay the information in the error form. For example:

if (isset($_POST['submit']) && $_POST['submit'] == 'submit'){//-> submitted - process data

$err = array();//initialize error array

if (isset($_POST['name']) && preg....){
$name = $_POST['name'];
} else {
$err['name'] = 'You forgot to enter your name';
}

if (empty($err)){//-> No errors

/////////--->> Process Here

} else {//Errors - show error form

if (!empty($err['name'])) echo $err['name'];
?>

Enter Your Name<br>
<input name="name" type="text" value="<?php if (isset($_POST['name'])) echo $_POST['name'];?>" size="30" maxlength="30">

<?
}

} else {//-> not submitted - show form

?>
<html>
<body>
<form method.......

Enter Your Name<br>
<input name="name" type="text" size="30" maxlength="30">

</form>

<?
}
?>

sakredei

7:49 am on Dec 10, 2006 (gmt 0)

10+ Year Member



thanks so much for your help!