Jump to content

Keeping form values in tact when returning errors


This topic is 5545 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hello,

When submitting a form to another page or itself and an error is returned (either by FM or verification code), is there an easy way to keep what the user had filled in in-tact when reloading the form page to display the error/message..besides using sessions?

Thanks for your help!

Link to comment
Share on other sites

Yes and No.

PHP maintains several predefined variables. Three which are relevant here are: $_POST, $_GET, and $_REQUEST. ($_REQUEST contains $_GET + $_POST + $_COOKIE);

They are associative arrays using the name attribute from the elements on the submitted form as keys to the elements' values. You can use these to repopulate your form on a submit error. A simple trick is to have the form submit to the same page. If the submission is successful then show the successful submission HTML; if the submission fails show the form.

The one caveat is that PHP does use sessions to maintain these variables. It manages the sessions so there is no need to worry about calling session_start(), but it does require cookies so that it can grab the session id from the client.

Link to comment
Share on other sites

Thanks for the reply. That makes sense....

If I understand correctly, I would preload the form fields with $_REQUEST['formfieldname'] - which wouldn't show on initial load but would once they submit, POSTing those variables, and reload the form with an error and submitted information.

Does that sound about right? Or would that throw an error on the initial load?

Link to comment
Share on other sites

That will throw a warning; do this:

<?

$field1 = isset($_REQUEST['field1'])?$_REQUEST['field1']:"";

?>

<?

Side note to Mods:

the code tags are still stripping ?> from code this makes them somewhat problematic for displaying code that switches between php and html

Link to comment
Share on other sites

This topic is 5545 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.