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.
|