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

Hi there, I have to call a perl script from the click of the button and do soemthings with the parameters passed and get back to the same page where i was... if i am using the javascript i would do document.refresh for this but what do i do if i want to do the same from perl. I tried
print "Location:http://mypage.html \n\n";
but the problem its clearing up the text feilds i already filled up its not refresing it... its getting as new page.
so i want to call the perl script onclick of button which will do the some things with parameters and then refresh the page with all the feilds filled in. Hope any one can help me with this.... thanks in advance. - anjaana

Replies are listed 'Best First'.
Re: How do i refresh a page from the perl script.
by thabenksta (Pilgrim) on Jul 02, 2001 at 22:35 UTC

    Assuming that the cgi script is what's creating the form and that you are using CGI, you could do something similar to the following.

    #!/usr/local/bin/perl -w use strict; use CGI my $q = CGI::new(); print <<HTML; <form action="name of this script.cgi"> Name: <input type="text" name="Name" value="$q->param{'Name'}"> Address: <input type="text" name="Address" value="$q->param{'Address +'}"> <input type="submit" value="Refresh"> </form> HTML

    Basically, if $q->param{'Address'} exists, it will fill that in as the value in the form.

    Hope that helps.

    -thabenksta
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
      With -w enabled you can't use params like that without getting uninitialized value warnings when the params aren't defined. Also $q->param{'Address'} shouldn't have curly braces but parentheses: $q->param('Address').

      This version shows a way to properly initialize the variables whether the params exist or not.

      #!/usr/local/bin/perl -w use strict; use CGI; my $q = CGI::new(); my $name = $q->param('Name') || ''; my $addy = $q->param('Address') || ''; print $q->header; print <<HTML; <form> Name: <input type="text" name="Name" value="$name"> Address: <input type="text" name="Address" value="$addy"> <input type="submit" value="Refresh"> </form> HTML

      --
      Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.

      Hi there thanks for the response. let me be more clear with my problem. I have a pure html page(not cgi). I have 3 buttons on it. first one is the submit and the second one is of type button and the third one is of type button. so the second one is calling the perl script. now this perl script does somethig with parameters i pass. and then gets back to the original page with :
      print "Location:http://mypage.html \n\n";
      but the thing is it doesnot refresh the page so all the fields are gone. Is it possible to just refresh a page from perl and not to restart the whole page again. thanks - anjaana
        The answer to the direct question is "no", and that has more to do with the way HTTP and web browsers work than Perl. In general, a web server doesn't normally send just part of a page to replace an existing page. There are ways to do this with JavaScript and other client-side programs as well as with multipart transfers. That's probably not what you want.

        A simpler explanation is to generate the HTML page within the CGI program. You'll send the whole thing to the browser, but you'll have the opportunity to substitute in your preferred parameters. If you follow this road, you might end up coding the whole thing as a Perl program, integrating the static HTML page into the program or as a template or something. If there are no parameters passed (that is, the user pressed no buttons, just loaded the page for the first time), send a blank form. If the user pressed a button, perform the appropriate action.

        Doing much of anything else makes a lot of assumptions about the web browser. In some cases, you can get away with that.