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

How to check value of checkbox on form submit?

by mt2k (Hermit)
on May 10, 2000 at 18:31 UTC ( #10955=perlquestion: print w/replies, xml ) Need Help??

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

This is a pretty easy (for you) and dumb question, but...
How can I check the value of a checkbox after an html for is submitted.
Shouldn't I be able to go ($input contains my html form content:
if ($input{'checkbox1'} eq "on") {

I have even tried:
if ($input{'checkbox1'} ne "") {
Help me!!!

Thanx

Replies are listed 'Best First'.
Re: How to check value of checkbox on form submit?
by cei (Monk) on May 10, 2000 at 21:54 UTC
    All of this is assuming, of course, that you are passing a value to $input. You didn't really specify how you were parsing the form results. One way is the following:
    #!/usr/bin/perl -w use CGI; use strict; my $page = CGI::new(); my $input = $page->param('checkbox1'); if ($input eq 'on') { # do something } else { # do something else } exit (0);
    with the HTML set like the others have suggested, giving a value of "on" to the checkbox named "checkbox1".
Re: How to check value of checkbox on form submit?
by comatose (Monk) on May 10, 2000 at 18:45 UTC

    Checkboxes don't have values unless you give them one. If they are checked, the name=value pair is sent. Otherwise, IIRC, nothing is sent. So in your HTML, you need:

    <input type="checkbox" name="blort" value="foo">
    When you receive your form input, "blort" will equal "foo" if checked. If it isn't checked, "blort" will be undefined.

      In other words, your form will work if your HTML is exactly like this:
      <input type="checkbox" name="checkbox1" value="on">
      Then value=pair in this case would be checkbox1=on (if checked obviously). I disagree that if not checked your $input{'checkbox1'} would be undefined. That would vary depdending on the query string parsing method. It may be null, but not necessarily undef. This might be a source for errors down the road.
        If the checkbox is not checked then it will not get posted to the query string. There will be no reference what-so-ever to the paramater (checkbox1 in this case). So the exists function mention below would probably be the easiest.

        Maybe this is just with my server (Apache), but I dont think so.
        However, just setting a value of on in the html and then checking for that value in the script should always work. Whether undefined or null, it definately won't equal the string "on" so...
Re: How to check value of checkbox on form submit?
by chromatic (Archbishop) on May 10, 2000 at 19:24 UTC
    How about:
    if (defined ($cb_value = $input{checkbox1}) { # do something } else { # do something less exciting }
    That will give you the value, if it's defined, or let you throw an error or rerequest the data, if necessary.
Re: How to check value of checkbox on form submit?
by plaid (Chaplain) on May 10, 2000 at 20:30 UTC
    You might want to take a look at the exists function. It does exactly what you're looking for: tells you if a hash key exists, regardless of what its value is.
    if(exists $input{'checkbox1'}) {
    This will work regardless of what the value of the checkbox is, as the hash key 'checkbox1' will not exist unless the checkbox is checked.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-07-01 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (98 votes). Check out past polls.

    Notices?