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

All

I seem to have a problem with using CGI, when i run the following code.

So the user selected an option from an HTML drop down list, this list information is forward to the program, which then displays that pages that the user asks for, by running the following routine

But when the this page is displayed by my prog,

sub displayreg() { print $q->start_form; print "<b>Please enter the details below to register a new user </b>" print "<EM>Users Username from </EM>"; print $q->textfield('username') +; print $q->submit(-name=>'Action',-value=>'Submit',-onClick=>&registeru +ser()); print $q->reset; print $q->endform; print "<BR>"; }

So when i create another routine down the page to enter the details into a textfile on the system or email it, it does not work

sub registeruser() { printf ("The username you entered was $q->param('username')); }

Replies are listed 'Best First'.
Re: CGI submit running sub routine
by herveus (Prior) on Sep 01, 2004 at 14:11 UTC
    Howdy!

    I'm a lot puzzled by this question. What exactly *is* happening? What are you expecting to happen?

    Do you expect your Perl subroutine "registeruser" to be called as a consequence of the "-onClick" attribute? Isn't that a JavaScript thingy?

    yours,
    Michael
Re: CGI submit running sub routine
by Grygonos (Chaplain) on Sep 01, 2004 at 14:33 UTC

    Usually a form will call a script with parameters from the form as CGI parameters that are retrievable via $cgi->param('my_param');. You will be making a POST request usually (in my experience anyway). It can even call the same script (I don't know how good/bad of an idea that is), so long as the script can determine who called it, you would be ok.

    Say you have a hidden CGI parameter from that form called source. You'll make the value of that parameter "register", so that when the script checks the value of source it knows roughly what parameters it can expect to receive, and can take action accordingly. This is just an idea. the suggestion you are making is roughly "Javascriptish" in that the action would happen immediately and not need to be reposted, but as with server side scripting, the request needs to be made to the server with the new information in order to properly handle your request.

Re: CGI submit running sub routine
by MCS (Monk) on Sep 01, 2004 at 15:55 UTC

    Basically, you can only use perl functions with perl... you can't use perl functions in html.

    To make this work, I would first check the parameter and if it exists, then print out your information, otherwise just display your form. Alternatively you can just launch a new cgi page, it all depends on what you want to do.

    Just remember, your perl code generates html. A drop down list is part of that html. You can take the parameter of that dropdown and parse it in another form or the same form but you have to resubmit the form. (which you can do on selecting an item from a dropdown)

Re: CGI submit running sub routine
by muntfish (Chaplain) on Sep 01, 2004 at 14:17 UTC

    If I've read your code snippets correctly - that's not the way to call back to the CGI script from a form.

    The -onClick parameter specifies a client-side (typically JavaScript) function which your browser will attempt to call when the user clicks the submit button. If you haven't defined such a JS function, nothing will happen.

    You need to make the Submit button send the request back to your CGI script running on the web server. I don't know the syntax for this when using the CGI module, but I suspect you need to add some detail to the call to start_form, specifically an ACTION which will be the URI of your CGI script containing the registeruser() function.

    P.S. Please put code in <code> tags to make it easier to read.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&