Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

cgi check button

by bigup401 (Pilgrim)
on Apr 24, 2015 at 10:01 UTC ( [id://1124505]=perlquestion: print w/replies, xml ) Need Help??

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

is there any way i can check if the submit button pressed or not using cgi

like

if # submit button press do the rest else #submit button not pressed don't do the rest

this is the html simple

<!DOCTYPE> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>Untitled Document</title> </head> <body> <p>firstname <input type="text" name="textfield" /> </p> <p> lastname <input type="text" name="textfield2" /> </p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </body> </html>

Replies are listed 'Best First'.
Re: cgi check button
by marto (Cardinal) on Apr 24, 2015 at 10:07 UTC

    Your example has no chcekbox. It's just a parameter passed back to CGI. The submit button has no associated form. How do you propose to do anything with CGI here? I suggest you read and understand the documentation for the tools you've chosen to use, and make more effort when asking for help.

    If this is a real example of something you're trying to do then Perl can't help you here. Everything is client side. Use JavaScript.

    Update: Strike out, substantial edit.

Re: cgi check button
by jeffa (Bishop) on Apr 24, 2015 at 15:28 UTC

    Yes, there is a way to check if a form was submitted via CGI. You need three things:

    1. a way to generate the HTML form
    2. a way to accept the submission of that form
    3. a way to generate the results of submitting that form
    By using template generation (such as HTML::Template) you can knock that down to two steps: display some HTML and deal with submitted form parameters.

    The trick is to use the presence of the "submit button's" parameter: if it is not found in the query parameters then the client has not submitted the form, so you display the form. If the "submit button's" presence is found in the query then you display results (or form submission errors that need to be corrected). Templates are smart in that they also have conditionals, so you can put the logic there instead of adding more code to your CGI script.

    Here is an example using HTML::Template and Plack (via Plack::Request and Plack::Response). You run this via plackup and what's ultra nice is that you can just drop this code into an Apache(2) enabled cgi-bin directory (use a .cgi extension) and it runs like you would expect any old .cgi script to. (See Plack::Handler::CGI.)

    use strict; use warnings; use Plack::Request; use Plack::Response; use HTML::Template; my $app = sub { my $tmpl = HTML::Template->new( filehandle => \*DATA, die_on_bad_params => 0, ); my $req = Plack::Request->new( shift ); my $query = $req->parameters; $tmpl->param( $query ); my $res = Plack::Response->new( 200 ); $res->content_type('text/html'); $res->body( $tmpl->output ); $res->finalize; }; __DATA__ <tmpl_if submitted> <h1>Hello <tmpl_var textfield> <tmpl_var textfield2></h1> <tmpl_else> <form> <p>firstname: <input type="text" name="textfield" /</p> <p>lastname: <input type="text" name="textfield2" /</p> <p><input type="submit" name="submitted" value="Submit" /></p> </form> </tmpl_if>

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: cgi check button
by marinersk (Priest) on Apr 24, 2015 at 13:21 UTC

    Hello, bigup401.

    I am curious about a few things -- it looks like you might be taking your first dip into web form handling with Perl using the CGI module. Is that correct?

    If so, there's a few things you might need to clarify.

    As marto already indicated, you have <input>tags, but they are not inside <form>tags. I'm not terribly familiar with the inner workings of CGI, but I would interpret some of what marto says to mean that CGI might be limited to stuff in forms. I've only ever used it that way, so I wouldn't have any reason to question that.

    Part of your question confuses me, though. You indicate you wish to make a decision on what to do based on whether or not a button has been clicked. Unless I'm missing something, your Perl script using CGIonly starts to run once the button has been clicked -- so I don't understand what you are trying to accomplish.

    Can you bridge the gap in our understanding with a slow, patient, thorough, careful, and detailed explanation of what you've done, what you're trying to do, and which piece has you stuck?

    Sorry to be so verbose, but I think your problem is conceptual, not code-related, so we need to get past that first.

      ok let me make it clear

      i want to disallow auto code execution

      thats why i asked if there any way i can do it. with submit button and make it manuel execution. because i have post and get in my script once the page reloads the script sends requst immediately to server. thats why i want to control that auto execution with submit button

      thats why i made simple html form! sorry i missed to put form tag. bt its example.

        I'm sorry, bigup401, we're just not connecting here.

             i want to disallow auto code execution

        I realize you probably think this is clear -- but it is not. It begs many questions, some of which are:

        1. What code are you trying to prevent execution of?
          • Perl CGI code from the server?
          • JavaScript code on the page?
          • JavaScript or other code entered into the form by the end user?
          • Some other kind of code?
        2. At what point are you trying to prevent this code execution?
          • Before the web page is fully displayed?
          • After the web page is displayed but before the user starts typing?
          • After the user starts typing but before they click the button? (This seems the likely answer but nothing is clear here)
          • Some other point in the user's experience?

        I wasn't kidding when I said to be slow, careful, detailed, and thorough. We are missing on more points than we are connecting on.

        Slow down, breathe deeply, maybe have a cup of hot cocoa. Then, relax, and try to explain the process you are expecting to see, and what you are trying to affect.

        i want to disallow auto code execution
        You're still being unclear.
        Do you mean you want to force people to use only your form in a browser only (and prevent, say, the use of LWP etc.)? If so, give up all hope, since people will find a way to automate your form anyway.

        Or do you mean that you want to know whether or not the submit button was pressed? If so, then you're halfway there. You have:
        <input type=submit name="make ponies appear" value="Submit button label">
        In your script, you would do:

        my $q = new CGI; if($q->param("make ponies appear")) { # do something with the form data } else { # show the form or something else }

        Or do you want something else? You'll need to be more specific.

        -Thomas
        "Excuse me for butting in, but I'm interrupt-driven..."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-25 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found