473,473 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Checkboxes: Setting POST values if unchecked

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.

Apr 23 '07 #1
4 25246
On Apr 23, 9:16 pm, Robert S <robert.spam.me.sensel...@gmail.com>
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.

Apr 23 '07 #2
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
Apr 23 '07 #3
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.
Apr 23 '07 #4
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.

Apr 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Claire | last post by:
Hello, I am having a problem in my struts application with the checkboxes in my form. I have an array of checkboxes, some of which may be already selected when the form loads. My problem is when...
6
by: smart_aleck72 | last post by:
I am trying to set up a pair of checkboxes where only one may be checked at a time BUT both should be able to unchecked as well. Thus an option group does not work as at least one value will...
1
by: kannadasan | last post by:
Hi all I am using Datagrid where i place checkboxes in one column with some other columns.The purpose is, if i select the checkboxes and clicks the submit buton Email has to go to the selected...
15
by: Sonnich | last post by:
Hi all! I have a file where I'd have to submit buttons, and I need to differ between those. Step #1 - open html win with default selection Step #2 - user may select some criteries for the...
3
by: somaskarthic | last post by:
Hi How to get the values of dynamically created checkboxes which are checked and unchecked (for both the conditions). Only the selected checkbox values are posted as 'On' . How can i get the value...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
3
by: mountain.dog | last post by:
I have a query that shows a list of options that a user can toggle on or off using a checkbox. query... form... while($row = mysql_fetch_array($result))... <input name="menu_show_attribute"...
1
by: tamari | last post by:
I have a strange problem changing the checked value of dynamically created checkboxes within code. This is what is happening. Depending what is selected I create checkboxes within a panel. These...
3
sunbin
by: sunbin | last post by:
Hi, I am having in a Trouble when working with dynamic checkboxes (i.e. checkboxes with the same name, e.g. <input type="checkbox" name = "check" value="dynamic integer value">) I have...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.