Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: How to implement a checkbox that will send data to db

by dasgar (Deacon)
on Jul 09, 2014 at 17:35 UTC ( #1092945=note: print w/ replies, xml ) Need Help??


in reply to How to implement a checkbox that will send data to db

Not sure that I would agree with using a checkbox for this situation. An HTML checkbox field allows the user to make 0 or more selections. If you do want to use a checkbox, you need to decide a true/false value for when it is checked and a true/false value when it's not checked. In your code to parse the HTML form input from the user, the checkbox's name will only show up in the paramater list if it was checked. So you'll need code to first determine if it is in the parameter list.

Personally, I would recommend going with radio buttons instead. HTML radio buttons allow the user to select only 1 value and does allow you to preset a default value. In this case, your values would be true and false. Also, your radio button value will always be in the parameter list.

Also, I should mention that this sounds more like an HTML design question rather than a Perl question. Are you having problems with Perl code to create the HTML form? Or Perl code to deal with user input from the HTML form? Or both? Or something else?


Comment on Re: How to implement a checkbox that will send data to db
Re^2: How to implement a checkbox that will send data to db
by terrykhatri (Acolyte) on Jul 09, 2014 at 17:45 UTC
    Hi, Ok I will go with radio button, if you can help me with for both perl routine and html. Many thanks for your response !! Rgds Terry

      You've been given a great deal of help, code and advice already, much of which you have chosen to ignore. What have you tried? What errors did you get? What part are you stuck at?

Re^2: How to implement a checkbox that will send data to db
by terrykhatri (Acolyte) on Jul 09, 2014 at 18:52 UTC
    Hi, I did try to implement it using a list box :
    # get Discontinued my $sql = qq!SELECT DISTINCT("Discontinued") AS disc FROM "Products" ORDER BY 1 DESC !; my $dr = $dbh->selectall_arrayref($sql); # Make up a pulldown menu for Discontinued my $discs = qq!<option value="">Select option</option>!; for my $row (@$dr) { $discs .= qq!<option value="$row->[0]">$row->[0]</option>\n!; }
    Discontinued :<select name="discontinued"> $discs </select><br/>
    But if I select 1 i.e. True the update goes through and if select 0 i.e. False it sends a null value which is throwing the following error :
    DBD::Pg::db do failed: ERROR: invalid input syntax for type boolean: +"" at /usr/share/perlproj/cgi-bin/editprod.pl line 44
    Any I idea why it will send 1 but not 0 ? Many Thanks Rgds Terry
      Fixed it by getting the results in character 'f' and 't' ( instead of numeric of 0 and 1) from database by changing the query.
      # get Discontinued my $sql = qq!SELECT DISTINCT("Discontinued")::CHAR AS disc FROM "Products" ORDER BY 1 DESC !;
Re^2: How to implement a checkbox that will send data to db
by peterp (Acolyte) on Jul 09, 2014 at 22:29 UTC

    I don't agree with your advice. Checkboxes are designed to handle boolean values and a checkbox is perfectly suited for this situation, think of common "do you accept these terms" checkboxes. Handling a checkbox with CGI is as easy as the following...

    my $boolean = $cgi->param('boolean') || 0; # or better: my $boolean = $cgi->param('boolean') ? 1 : 0; # etc

    Afterall, even if a true/false pair of radio buttons were used, user input cannot be trusted and the parameter would have to go through the same / similar process to above.

Reaped: Re^2: How to implement a checkbox that will send data to db
by NodeReaper (Curate) on Jul 09, 2014 at 22:29 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-12-27 11:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls