473,765 Members | 2,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Checkboxes: Setting POST values if unchecked

I have a form with a checkbox:

<form method="post" action="test.ph p">
<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 25258
On Apr 23, 9:16 pm, Robert S <robert.spam.me .sensel...@gmai l.com>
wrote:
I have a form with a checkbox:

<form method="post" action="test.ph p">
<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.ph p">
<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.ph p">
<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
6038
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 the user then deselects one or more of these checkboxes and submits the form - they're being submitted as if they were still checked, when the user has infact deselected them. I've heard that this is a common problem, but I don't seem to be...
6
3992
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 always be true. I tried: Private Sub chkRMA_Click() chkRMAOther = False End Sub
1
2050
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 Categories. The problem here is after clicking submit button i found the checkboxes are not unchecked (cleared) even though i put EnableViewState="False".
15
8840
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 list Step 2 might be repeated, or not used at all. There is a button "apply selection"
3
5625
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 of unchecked checkboxes ? Each row contains 7 checkboxes. These rows are created dynamically by clicking a button. When the page is posted , i need to get the values as row1 checkbox1 = selected checkbox2 = notselected checkbox3 = selected...
10
5207
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. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named 'CheckAllRows' on the CheckedChanged event of this CheckBox. The CheckBox in the Header exists within the HeaderTemplate of a TemplateColumn in the...
3
5157
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" type="checkbox" class="checkbox"'); if ($row == 1) { echo (' value="'.$row.'" checked />
1
2447
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 work very well if I click on them manually, but sometimes I need to check or uncheck some of the checkboxes depending on further input. To change the checkboxes I look at the tag value and from within a loop of the controls. I then can change the...
3
2606
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 submitted an HTML form (which contains dynamic textboxes and dynamic checkboxes with a Post Button(input type="button")) by using Ajax function. (onClick="return ajaxFunction(document.getElementById('formid'));") In the ajax function, i have collected...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.