Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Problems with sticky CGI params

by hoonz (Initiate)
on Oct 23, 2001 at 16:48 UTC ( [id://120767]=perlquestion: print w/replies, xml ) Need Help??

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

I have a single perl script that has - in one of its modules a form that enables the user to change viewing preferences. The form then calls another module to update a cookie.

sub display{ print <<"EOF"; <FORM action="forum.pl?quicklog" method="POST"> ...(form stuff here)... </form> EOF }

sub quicklog{ ....store cookie stuff here.... ....then I tried the following.... print "$redirect\n\n"; }

The $redirect is set to: "http://www.mysite.com/cgi-bin/forum.pl"

When the quicklog sub-routine is called it goes to url: "http://www.mysite.com/cgi-bin/forum.pl?quicklog"

The print statement does not redirect to "http://www.mysite.com/cgi-bin/forum.pl"

I'm trying to get the script to in effect reload itself without the ?

Any ideas? I'm stuck and still not a guru :)

Edit kudra, 2001-10-23 Changed title per ntc request

Replies are listed 'Best First'.
Re: humble apologies - not a master
by projekt21 (Friar) on Oct 23, 2001 at 16:53 UTC

    Are you sure you don't want to say:

    use CGI; my $query = new CGI; # ... snip ... print $query->redirect($redirect);

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

      The code is existing code and not in modperl? (which I think is what you are talking about?)

      Any way to do it in old perl?

        I'm not sure about this, but you need to send a redirection header which should be:

        print "Location: $redirect\n\n";

        P.S. I wasn't talking about mod_perl, just about CGI.pm.

        alex pleiner <alex@zeitform.de>
        zeitform Internet Dienste

Re: Problems with sticky CGI params
by Hero Zzyzzx (Curate) on Oct 23, 2001 at 17:27 UTC

    hoonz, you can't just print a URL and hope for a redirect, you have to print a properly formatted redirect header.

    What your script prints is just the url, "http://www.mysite.com/cgi-bin/forum.pl". That isn't enought to cause a browser to redirect, it has to look something like "Location: http://www.mysite.com/cgi-bin/forum.pl\n\n". Headers like these are easy to screw up- browsers care or don't care about capitalization and newlines with no rhyme or reason.

    Projekt21 showed you the "correct" way to do this, and his/her version has nothing to do with mod_perl. Using CGI to print headers will save you much time debugging, and will create what you can be sure of are good headers.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      Thanks Projekt21 and Hero. I tried the print Location version and it displays a 404 error (page not found)after calling the sub

      The script creates HTML on the fly and does not use HTML templates. Does CGI.pm have to be installed on the server or can I have it in my CGI bin? If I do use it, do I have to re-code everything or can I just do parts?

        CGI.pm is part of the standard perl distribution so should already be installed. You can install virtually any module locally but you should not need to. No major recoding is required. Just use what you want, CGI.pm is a huge module and few people use all its many features - at least in the one script. See Use CGI or die; and No excuses about not using CGI.pm

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        More than likely, CGI.pm is already installed on your system. If for some reason it isn't, yes, it would probably make life a little easier for it to be installed, but you can use a private copy, provided you use lib at the top of your script. For example:
        #!/usr/bin/perl use lib "/my/module/directory"; use CGI;

        Perl will then look in your specified directory for the module before looking through the the default directories in @INC.

        As far as re-coding if you use CGI.pm goes, the short answer is technically, no, you don't have to re-code everything. Others may disagree with me, but personally, I'd be inclined to just use CGI.pm for the parts in question for the time being. Once you get it working, dig into CGI.pm a bit further and see if there are ways to make your script better and more efficient by taking advantage of more of the features of CGI.pm.

        That's my 8**(1/3) cents. Hope that helps.
        ___________________
        Kurt

        I'm sure you're getting a 404 error because there's either something wrong with your header or your URL is wrong (most likely the latter, IMO). Your redirect header also needs to be the first thing printed from your script.

        To answer your other questions: If you're using perl, you can use CGI in most cases. The CGI perl module is part of all modern perl distributions. And in terms of using "parts" of CGI- yes, you can. Another lovely thing about perl is that it doesn't force you into one way of doing things. Why don't you try the code that Projekt21 suggested and see what shakes out?

        -Any sufficiently advanced technology is
        indistinguishable from doubletalk.

Re: Problems with sticky CGI params
by mkmcconn (Chaplain) on Oct 24, 2001 at 02:09 UTC

    If you use CGI, the module helps you to override sticky values, using the "override" attribute of the field element.
    I apologize for not knowing exactly what you are looking for, so that my example can be simply plugged in as-is. But, maybe it can be helpful in a general way.

    $out->textfield('_variable' ,$out->param('redirect')?$ref->{'url'}:'qu +icklog',20,-override => 1)
    '_variable' is where the field value is stored.
    $out is the name of the CGI object.
    $out->param('redirect')?$ref->('url') is the parameter that expands to the default URL, over which I want to force changes using the form. And here comes the answer to your question (hopefully):
    -override => 1 allows the default parameter to be overridden by the submitted form contents.

    If you don't want to use CGI, you may still find this to be a good clue - but I don't have much experience trying to create form and field code without CGI.pm :-)

    mkmcconn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://120767]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-20 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found