Gossamer Forum
Home : General : Internet Technologies :

Submitting unchecked checkboxes in forms

Quote Reply
Submitting unchecked checkboxes in forms
Hi there.

Has anyone ever come up with a good solution for tricking a cgi form into submitting an unchecked checkbox as a "null" value, instead of the default where it doesn't get submitted at all?

I'm thinking maybe javascript is the way to do this, and I'm contemplating attempting it myself (which could get ugly - my javascripting chops are meager to say the least). But I figured this is probably a common enough problem that someone somewhere must have already solved it, and I see no reason to reinvent the wheel (particularly when I'm liable to come up with a lumpy, square-shaped one that barely rolls... Wink).

Anyway, love to hear if anyone has any experience with this.

Thanks in advance.

Best regards,
Adam

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [adamuforestasan] Submitting unchecked checkboxes in forms In reply to
With JavaScript you could do...

Code:
<javascript>
function checkMe() {
if (form.CheckBox.checked) {
form.CheckBox.value = "Yes"
} else {
form.CheckBox.value = "No"
}
}</javascript>

In your html you'd have to specify you checkbox value as "1". My limited experience has been that if you use any other value the javascript won't work. But in the JavaScript you can set the value to anything you like (on/off, yes/no, 1/0)

<input type="checkbox" name="CheckBox" value="1">

Of course you'd need some way to trigger the script such as adding "onClick="checkMe()" to a submit button (however there are caveats to this - do a search if you decide to go the submit button route) or someother type of Event Handler such as onBlur, onKeyUp, onFocus, etc.

Good Luck.

Ps: there are perl solutions as well, but I had the javascript one handy - the perl solution will work pretty much using the same logic (if/then and being triggered by some function/subroutine).
Quote Reply
Re: [Watts] Submitting unchecked checkboxes in forms In reply to
Thanks! That seems simple and straightforward enough - I just wasn't sure of the syntax.

You mention perl solutions... Interestingly, I believe DBMan has this functionality built in (or at least there's a standard "checkbox generator" mod that does, not sure which) but DBManSQL is lacking. Alas...

Anyway, thanks.

Adam

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [adamuforestasan] Submitting unchecked checkboxes in forms In reply to
You can alsow define a default value in the script for your checkboxes.
The moment a user checks the box the default value in overwriten.

Something like......

Code:

my $answer = "No"; $answer = &get_value_checkbox($checkbox_one);
Quote Reply
Re: [Watts] Submitting unchecked checkboxes in forms In reply to
Setting the value to No doesn't seem to do anything, because, unless the box it's checked it's not going to be passed to the params using cgi.pm, or, I can't figure it out? Has anyone gotten this javascript code to work?

Thanks,
Wassim
Quote Reply
Re: [wassimk] Submitting unchecked checkboxes in forms In reply to
Here is one solution that will post a value even if the checkbox is not checked. The compromise is that it requires a hidden input field to help it...

<input type="checkbox" name="_Active"
onChange="javascript:if (objForm._Active.checked) { objForm.Active.value='true'; } else { objForm.Active.value='false'; } "
value="x" checked >


<input type="hidden" name="Active" value="true" >

Basically I use the check box for the UI, its value is not used on the server side, the check box UI updates the hidden field when ever its changed and the server side picks up the right value in the hidden field as reflected by the check box. Just make sure u initialise both input field at the start.



Tongue Hope that helps folks......