newbie00 has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

For form field validation, how do I do the following where the field validation is contained in the file called by the existing <form action...>? This file displays a page with the message 'Please press Back and try again.' once validation has failed. Upon pressing the 'BACK' button, the fields are EMPTY and the user must start from scratch and therefore is a turnoff.

1) How to STORE form field values and return them back to their form fields AFTER using the 'BACK' button once form fields failed validation.

Is there a way to re-capture/recall those field values (pass those variables back) e.g. via enviromental values (for ANY browser) to the previous page?

This is needed for a pre-existing script.

2) How to do this validation for those browsers NOT using cookies and/or javascript?

3) If either cookies or javascript must be used, which is more prevalent for a browser to have enabled?

Thanks in advance.

  • Comment on How to re-capture/recall form field values AFTER pressing the 'BACK' button?

Replies are listed 'Best First'.
Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by CountZero (Bishop) on Jul 12, 2006 at 21:07 UTC
    Pressing the BACK button in your browser will not force it to go back to the server to fetch that page again. It will simply display its cache for this page.

    The canonical way of validating user input server-side is to return the same page to the user with all values intact and the fields which did not pass validation appropriately marked.

    Most modules with FormValidator in their name do this more or less automatically or at least provide lots of functions to assist you in that respect.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by Joost (Canon) on Jul 12, 2006 at 20:45 UTC
Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by ptum (Priest) on Jul 12, 2006 at 21:00 UTC

    This isn't a Perl question, but I'll address it anyway. :)

    In general, HTML forms with the same-named controls will be 'sticky' such that the previous values entered will be populated in a subsequent display of the form.

    Usually the way validation errors are handled is to say, "Validation failed for reason X" and re-display the form (often with the label to the offending field bolded), rather than asking the user to press 'Back' (as was noted in Joost's response).


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by newbie00 (Beadle) on Jul 12, 2006 at 21:29 UTC
    Thanks for your replies.

    I prefer not to use the 'BACK' button and the scripts I write typically perform the validation within the script by using the script's URL in 'form action'. I'm using a pre-existing application and I'd like to revise it as little as possible.

    What I'd like is for something like that used here on the Perlmonks site. For example, if you forget to fill in the title when creating a new node, the content in the text box is preserved when the 'BACK' button is clicked. How is this done?

    Thanks.

      The browser does that.

        To be more specific -- it's a function of the browser, and each browser can handle it as they wish. Some older browsers will re-populate everything (including passwords), others will populate all but passwords, some will save no state at all.

        So -- you can't depend on anything. You're better off doing off as others have suggested, and regenerating the form with the info populated as you wish (censoring passwords, etc, as required). This also gives you an opportunity to mark any problem fields that need correction (which is the normal reason for needing to show the populated form with the previous values)

        Oh -- and because this is undefined behaviour for the browser -- there are a few browsers that won't cache a POST, so won't reshow you the form, and thus you can't use the back button if the form was generated from a POST request.

        ...without being instructed to do so by PerlMonks.
Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by edan (Curate) on Jul 13, 2006 at 11:00 UTC
    As everyone said, filling in the form is the way to go. I've used the HTML::FillInForm with success in the past, and it should be very easy to plug in to a pre-existing app that uses CGI.

    --
    edan

Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by newbie00 (Beadle) on Jul 13, 2006 at 18:45 UTC
    Thanks all for your responses.

    Based on these responses, I will most likely convert any form that doesn't reload the form with the previous values to doing so. It seems like the best way to go.

    Thanks again.

Re: How to re-capture/recall form field values AFTER pressing the 'BACK' button?
by Anonymous Monk on Jul 13, 2006 at 17:53 UTC