Checkboxes: Setting POST values if unchecked

R

Robert S

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

RobG

Robert said:
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.
 
N

naph

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

-Lost

RobG said:
Robert said:
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.
 
R

Robert S

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top