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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retaining form information on reload

Status
Not open for further replies.

shawnrberg

Programmer
Joined
Jun 8, 2004
Messages
1
Location
US
I have this question in half a dozen forums and I can not find an answer.

I have a php form that when a visitor overlooks a field, an error message is sent to the top of the page when they hit "Submit" telling them "Name Required", "Address Required", etc.

As it stands now, when the page reloads and the error message prints to the top of the form, ALL the form fields are reset to nothing; forcing the visitor to re fill the ENTIRE form.

Any code suggestions to retain the previously entered info so the visitor doesn't have to retype EVERYTHING?

I'M DYING HERE!

Sincerely,
shawnrberg
 
I use javascript to check the form before processing and returning a true or false to know if ok to continue the process. This as far as I know (which is not much by the way) is the best method.
 
you can use this for the value of your components. for example for a textbox
Code:
<INPUT TYPE="text" NAME="element_name" value="<? if(isset($_POST["element_name"])) echo $_POST["element_name"];?>">
 
When you submit the form, the values are stored either in $_POST or $_GET superglobal, depending on the method you are using in your form. The only thing you have to do is to populate your form elements with this values:
Code:
echo '<input type="text" name="address" [b][COLOR=blue]value="' . $_POST['address'] . '" class="myinputbox" />';

or 

echo '<input type="text" name="address" [b][COLOR=blue]value="' . $_GET['address'] . '" class="myinputbox" />';
depending on the method you are using. If the user did not fill the field it will show up blank, if they have, then the field will be populated with that value. Hope it helps.
 
You could even modify the entry before sending it back to the user.

Code:
<?php
// At the top of the form
$name = "";
if(isset($_POST["element_name"]))
   $name = trim($_POST["element_name"]);
?>
.
.
.
<INPUT TYPE="text" NAME="element_name" value="<?php echo $name;?>">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top