Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Checkboxes: Setting POST values if unchecked

Reply
Thread Tools

Checkboxes: Setting POST values if unchecked

 
 
Robert S
Guest
Posts: n/a
 
      04-23-2007
I have a form with a checkbox:

<form method="post" action="test.php">
<input type="checkbox" name="foo" checked>
<input type="submit">
</form>

If the checkbox is checked, $_POST['foo'] gets assigned the value
"on" (using php as the second file)

But if the checkbox is unchecked, $_POST['foo'] is not assigned.

Is there a simple way that I can set an arbitrary value to this
(eg."off"), if the checkbox is not checked? An onClick method for the
submit button would be a good start.

I'm sure this must be a common problem, but I can't find a simple,
relatively generic solution on the web.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      04-23-2007
Robert S wrote:
> I have a form with a checkbox:
>
> <form method="post" action="test.php">
> <input type="checkbox" name="foo" checked>
> <input type="submit">
> </form>
>
> If the checkbox is checked, $_POST['foo'] gets assigned the value
> "on" (using php as the second file)
>
> But if the checkbox is unchecked, $_POST['foo'] is not assigned.


You need to read the HTML specification again. Any control without a
value need not be submitted at all. An unchecked checkbox need not be
submitted either, so even if the checkbox "foo" is checked, it may not
be submitted because it has no value.

If it isn't checkeed, it very likely will not be submitted by any browser.


> Is there a simple way that I can set an arbitrary value to this
> (eg."off"), if the checkbox is not checked? An onClick method for the
> submit button would be a good start.


No, it would be a bad start. You can't depend on client-side scripting
doing anything.


> I'm sure this must be a common problem, but I can't find a simple,
> relatively generic solution on the web.


If your server gets no value for "foo", it wasn't checked. No client
side scripting, nothing fancy, just plain server-side logic.


--
Rob
 
Reply With Quote
 
 
 
 
naph
Guest
Posts: n/a
 
      04-23-2007
On Apr 23, 9:16 pm, Robert S <(E-Mail Removed)>
wrote:
> I have a form with a checkbox:
>
> <form method="post" action="test.php">
> <input type="checkbox" name="foo" checked>
> <input type="submit">
> </form>
>
> If the checkbox is checked, $_POST['foo'] gets assigned the value
> "on" (using php as the second file)
>
> But if the checkbox is unchecked, $_POST['foo'] is not assigned.
>
> Is there a simple way that I can set an arbitrary value to this
> (eg."off"), if the checkbox is not checked? An onClick method for the
> submit button would be a good start.
>
> I'm sure this must be a common problem, but I can't find a simple,
> relatively generic solution on the web.


the first question you need to ask is do you really need "off"? isn't
an empty string as good as off (maybe ur particular problem really
does need an "off", but it's something worth thinking about if you
haven't already).

rod.

 
Reply With Quote
 
-Lost
Guest
Posts: n/a
 
      04-23-2007
RobG wrote:
> Robert S wrote:
>> I have a form with a checkbox:
>>
>> <form method="post" action="test.php">
>> <input type="checkbox" name="foo" checked>
>> <input type="submit">
>> </form>
>>
>> If the checkbox is checked, $_POST['foo'] gets assigned the value
>> "on" (using php as the second file)
>>
>> But if the checkbox is unchecked, $_POST['foo'] is not assigned.

>
> You need to read the HTML specification again. Any control without a
> value need not be submitted at all. An unchecked checkbox need not be
> submitted either, so even if the checkbox "foo" is checked, it may not
> be submitted because it has no value.
>
> If it isn't checkeed, it very likely will not be submitted by any browser.
>
>
>> Is there a simple way that I can set an arbitrary value to this
>> (eg."off"), if the checkbox is not checked? An onClick method for the
>> submit button would be a good start.

>
> No, it would be a bad start. You can't depend on client-side scripting
> doing anything.
>
>
>> I'm sure this must be a common problem, but I can't find a simple,
>> relatively generic solution on the web.

>
> If your server gets no value for "foo", it wasn't checked. No client
> side scripting, nothing fancy, just plain server-side logic.


Yep. RobG is, as usual, right. That is why functions like isset()
exist in PHP for example.

if (!isset($_POST['foo'])) { print 'It prints *this* text, because
isset(), in this example, returns a Boolean FALSE.'; }

This was a basic PHP question.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
 
Reply With Quote
 
Robert S
Guest
Posts: n/a
 
      04-23-2007
> Yep. RobG is, as usual, right. That is why functions like isset()
> exist in PHP for example.
>


Thanks. I think I get the point now. Its probably better not to use
a checkbox and use another control like radio buttons if you want to
have a generic method to give a POST variable. The javascript would
fall over if the client had javascript disabled.

That no doubt explains the lack of information on the web about this.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Xlint " warning: [unchecked] unchecked conversion" RVic Java 19 07-25-2009 08:40 PM
checkbox remains unchecked.. =?Utf-8?B?SW50cyBWaWxrcw==?= Wireless Networking 1 02-28-2005 01:10 PM
C# Exception is Unchecked??? ChInKPoInt [No MCSD] ASP .Net 3 12-11-2004 12:15 PM
C# Exception is Unchecked??? ChInKPoInt [No MCSD] ASP .Net 0 12-11-2004 03:47 AM
Post post post. Shel-hed Computer Support 2 11-08-2003 07:41 AM



Advertisments