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

Why does post and back button erase form data

Status
Not open for further replies.

skw

Instructor
Jun 1, 1999
19
US
I have a problem with a form that uses Perl.

If the user misses a form field, in this case, the user misses to select from a drop-down list, clicks submit, the warning page comes up saying "missing data", I click back button, my textarea field box data that has my lengthy input is totally erased!

I've been reading that cookie is the answer but I cannot locate how I set a cookie that'll retain the form field data from the client side and how I specify this cookie in my Perl script that runs my form.

Can someone please help.
 
No one can respond or have a suggestion?
 
I think that is a browser specific behavior. Netscape 4.6 and 4.7 don't retained text inputs after a submit and back. Netscape 7 beta does retain them. IE 5 and 6 both retain them. If it is a real problem, then I would make your CGI app supply your users with an "Edit Your Responses" button or link and get away from the back button. Of course, there is no way to prevent a user from clicking that back button (actually, I guess you could use a little javascript to hide the button, but I digress). I can't write and post it immediately, but I'll try to write a short/simple example of dealing with this and get it up late today or maybe monday. (typing in the southeastern US).



'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
goBoating, thanks for the tip but we use IE5 browser, though most text I've read suggest that this is a problem with Netscape.

I see examples that say that the cookie must be set in the httpd header, as example shown below,

Content-type: text/html
Set-Cookie: cookiename=cookievalue; path=/; expires Day, dd-Mmm-YYYY hh:mm:ss timezone

I'm still uncertain if the cookie is a script that I create and that call it in my Perl script that handles the form using the above example. Or, if I insert the &quot;Set-Cookie:&quot; in the <head> section of the html form.

I will read the FAQ on how to make my subject title to get more audience. :D
Thanks.
 
Without employing a trick or two, CGI is totally non-persistent. It does not 'remember' anything from one http request to the next. Loading a data entry page into a browser is the web server satisfying the first 'request'. After that page is submitted, the web server has no 'memory' of it.

How to fix the problem?

Case 1:
You have small clearly defined data elements - use some javascript to check the inputs as they are filled in and don't let the submit work until all inputs are filled correctly.

Case 2:
Your inputs are to varied in content to check with js, and you need a little stronger tool to do the checking (Perl does that just fine). Strategy - build a CGI application that creates your data input page. Build the application so that it checks to see if it is handed any info. If so, use that info as the default content for your data entry page.

You could do this with cookies (I think) but hidden fields may be easier.

Code:
#!/usr/local/bin/perl 
use CGI;
use strict;
my @fields = ('manufacturer','model','color');

# create a CGI object
my $cgi = new CGI;

# start the html page and form
print $cgi->header,
      $cgi->start_html,
      $cgi->start_form;

if ($cgi->param('submit'))
    {
    # submit is populated - thus this request must
    # be the result of someone submitting the data
    # entry page.
    # show their input
    print $cgi->Dump;
    
    # for each input field, 
    # create hidden inputs containing the submitted 
    # info with a button to go back, if needed.
    foreach my $field (@fields)
        {
        my $value = $cgi->param($field);
        print qq(<input type=&quot;hidden&quot; 
                        name=&quot;$field&quot; 
                        value=&quot;$value&quot;>\n);
        }
    # create a button to let the user go back
    print qq(<input type=&quot;submit&quot; 
                    name=&quot;goBack&quot; 
                    value=&quot;Edit Inputs&quot;>);
    }
else {
    # no 'submit', yet.  We must need to give the user
    # the data entry page.
    # If this is a new request, $value will be empty
    # and the user will get empty input cells.
    # OR, if this is someone using the 'Edit Inputs' button
    # $value will contain their previous inputs as passed via
    # the hidden fields.
    foreach my $field (@fields)
        {
        my $value = $cgi->param($field);
        print $cgi->textarea(-name=>&quot;$field&quot;,
                             -default=>&quot;$value&quot;,
                             -rows=>'5',
                             -columns=>'35'),
              '<br />';
        }
    print qq(<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;submit&quot;>);
    }
print $cgi->end_form,
      $cgi->end_html;


This flow may be overly simple, but maybe it illustrates one possible approach to enable your users to 'goBack'. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top