Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: How to check value of checkbox on form submit?

by comatose (Monk)
on May 10, 2000 at 18:45 UTC ( #10958=note: print w/replies, xml ) Need Help??


in reply to How to check value of checkbox on form submit?

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.

Replies are listed 'Best First'.
RE: Re: How to check value of checkbox on form submit?
by BBQ (Deacon) on May 10, 2000 at 19:07 UTC
    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...
        You're right! I thought that sounded a little odd so I ran the following test:
        <html><body> <form action="http://johnny.warp.psi.br/cgi-bin/test.pl"> <input type="text" name="field1"> <input type="checkbox" name="field2" value="on"> <input type="submit"> </form> </body> </html>
        If this form is submitted with everything blank, curiously the text input gets carried as null, but the checkbox is undef! QUERY_STRING : field1=

        I don't see where the consistency is here, but that was something I had never really noticed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2022-08-11 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?