Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Keeping form values in tact when returning errors

Featured Replies

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!

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.

  • Author

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?

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

  • Author

Thanks! I'll give this a try.

  • Author

Baloo,

This worked great. Thanks for the advice.

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.