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

Hi there, I'm displaying data from a MySQL database,which can be changed and saved again (through an html-page with <form>). Only the problem is to show and save checkboxes. If a database value is true, I can show the checkbox checked,but when I 'uncheck' the checkbox, the value is still saved as checked. Other fields like characters is no problem. Jan.

Replies are listed 'Best First'.
Re: checkbox
by simon.proctor (Vicar) on Feb 12, 2002 at 10:11 UTC
    Without seeing your code I can only guess its that checkbox values are only passed to the cgi script when checked. If they are unchecked then they do not get passed at all. Unless your script checks that the checkbox value has not been passed and is hence false your program may decide not to update the value. As an alternative you may consider changing to using radio buttons. You can then have two radio buttons, one for true and one for false.

    The alternative is to use some form of Javascript manipulation to determine the value of some hidden field (one for each checkbox). Though this is more complicated than it sounds and isn't really scalable.

    Hope that helps
Re: checkbox
by rdfield (Priest) on Feb 12, 2002 at 10:41 UTC
    Remember that 'unchecked' checkboxes are not passed back to your CGI script. Your CGI script should set all values in the database to 'unchecked' unless the param is expicitly returned as 'checked'.

    eg

    $checkboxes{$boxname} = defined($params{$boxname})?"true":"false"; #where $params is a hash of the parameters, keyed by name #and $checkboxes is a hash of the values supplied to the sql

    rdfield

Re: checkbox
by mattr (Curate) on Feb 12, 2002 at 12:37 UTC
    It sounds like a problem that could be solved by reading the documentation to CGI.pm. Checkbox values are not sent to the server by browsers if they are not checked. This can definitely cause you problems.

    The answer is to know which form fields are to be expected, and set the values for unreturned checkboxes yourself, or you can try radio buttons or something else. I think it is only checkboxes which do so.

    Another way to solve this kind of a problem is to print out the data you have received from the browser at different points in its processing to narrow in on where the problem is. Of course, I figured it out myself by making the same mistake once..

    P.S. You need to type your responses as HTML to format them correctly here..

    Move SIG!

Re: checkbox
by mattr (Curate) on Feb 13, 2002 at 08:58 UTC
    (duh I answered a reaped node - moved it here where it's already been answered..)

    It sounds like a problem that could be solved by reading the documentation to CGI.pm. Checkbox values are not sent to the server by browsers if they are not checked. This can definitely cause you problems.

    The answer is to know which form fields are to be expected, and set the values for unreturned checkboxes yourself, or you can try radio buttons or something else. I think it is only checkboxes which do so.

    Another way to solve this kind of a problem is to print out the data you have received from the browser at different points in its processing to narrow in on where the problem is. Of course, I figured it out myself by making the same mistake once..

    P.S. You need to type your responses as HTML to format them correctly here..

    Move SIG!

Re: checkbox
by screamingeagle (Curate) on Feb 12, 2002 at 18:13 UTC
    Here's a function which checks if a variable has data or not.Before firing an SQL statement, use this function on all the parameters being passed :
    sub returndata_or_null { my $var = shift; $var =~ s/\'/\'\'/og; return ($str) ? "'$str'" : "null"; }