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

Hi, Now I have a situation where I have to use a check box. The db table column has a Boolean data type i.e. True or False
Column | Type | M +odifiers ----------------+--------------------+-------------------------------- +------ Discontinued | boolean | default false select distinct("Discontinued") from "Products"; Discontinued -------------- f t (2 rows)
Will appreciate if you monks help me implement it in my ADD and EDIT forms :
my @options = ("True", "False"); <form action="" method="post"> Discontinued :<input type="checkbox" name="discontinued" value=$option +s><br> </form>
If its checked it should send a TRUE value to the database else False. Many thanks in advance Rgds Terry Please Note : I will post it to PerlGuru as well.

Replies are listed 'Best First'.
Re: How to implement a checkbox that will send data to db
by dasgar (Priest) on Jul 09, 2014 at 17:35 UTC

    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?

      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.

      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 !;
      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: How to implement a checkbox that will send data to db
by Anonymous Monk on Jul 10, 2014 at 01:35 UTC