473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do get the state of checkboxes in php?

I have a group of checkboxes drawn by javascript and I need to get each
of their states into PHP after submitting the page. I should think I
could get them in a POST, but that fails. Anyone have any ideas?
TIA
Sep 7 '07 #1
7 9886
Lorenzo Thurman wrote:
I have a group of checkboxes drawn by javascript and I need to get each
of their states into PHP after submitting the page. I should think I
could get them in a POST, but that fails. Anyone have any ideas?
TIA
It's how HTML works. Only checked boxes are sent in the GET or POST
request. Unchecked ones are not.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 7 '07 #2
On Fri, 07 Sep 2007 13:06:34 -0500, Lorenzo Thurman wrote:
I have a group of checkboxes drawn by javascript and I need to get each
of their states into PHP after submitting the page. I should think I
could get them in a POST, but that fails. Anyone have any ideas?
TIA
Use an additional hidden input, with the same name as the checkbox and a
value of 0. In almost all cases you'll want the hidden input to appear in
the source before the actual checkbox.

When the form is submitted, if the checkbox has been checked, it's value
will override the hidden input of the same name. If it hasn't been checked
the hidden input (with its value of 0) will be in the $_POST array.

AFAIK this is the only way to do it as unchecked checkboxes are not passed
to POST at all.

--
Kelsey Sigurdur

Sep 7 '07 #3
On Sep 7, 5:02 pm, Kelsey Sigurdur <kel...@m-f-d.orgwrote:
On Fri, 07 Sep 2007 13:06:34 -0500, Lorenzo Thurman wrote:
I have a group of checkboxes drawn by javascript and I need to get each
of their states into PHP after submitting the page. I should think I
could get them in a POST, but that fails. Anyone have any ideas?
TIA

Use an additional hidden input, with the same name as the checkbox and a
value of 0. In almost all cases you'll want the hidden input to appear in
the source before the actual checkbox.

When the form is submitted, if the checkbox has been checked, it's value
will override the hidden input of the same name. If it hasn't been checked
the hidden input (with its value of 0) will be in the $_POST array.

AFAIK this is the only way to do it as unchecked checkboxes are not passed
to POST at all.

--
Kelsey Sigurdur


You could just test for the value in the post buffer and set a
variable:
$lnChecked = ($_POST['CheckboxName'] == "on") ? 1 : 0 ;

Best,

Tom.

Sep 7 '07 #4
Kelsey Sigurdur wrote:
On Fri, 07 Sep 2007 13:06:34 -0500, Lorenzo Thurman wrote:
>I have a group of checkboxes drawn by javascript and I need to get each
of their states into PHP after submitting the page. I should think I
could get them in a POST, but that fails. Anyone have any ideas?
TIA

Use an additional hidden input, with the same name as the checkbox and a
value of 0. In almost all cases you'll want the hidden input to appear in
the source before the actual checkbox.

When the form is submitted, if the checkbox has been checked, it's value
will override the hidden input of the same name. If it hasn't been checked
the hidden input (with its value of 0) will be in the $_POST array.

AFAIK this is the only way to do it as unchecked checkboxes are not passed
to POST at all.
This is the generic way to do javascript/php dialogue.

EITHER set your submit button to invoke javascript and cahin to the next
page using get variables.. i.e. replace.window("myurl?myvar=Myvalue");

OR set up a POST form with id hidden variables, do a
getElementbyId('myvar').value="Myvalue";
and then submit()....

Syntax NOT guaranteed ;-)
Sep 7 '07 #5
The HTML form:

<p>Please select every sport that you play.</p>

Soccer: <input type="checkbox" name="sports[]" value="soccer" /><br /
>
Football: <input type="checkbox" name="sports[]" value="football" /
><br />
Baseball: <input type="checkbox" name="sports[]" value="baseball" /
><br />
Basketball: <input type="checkbox" name="sports[]"
value="basketball" />

---------------------------------------------------------------------------------------

The php script:
$sports = $_POST['sports']; // is an array holding all the checked
boxes

foreach ($sports as $checked_sport){

echo $checked_sport."<br />"; // Output the checked boxes to the
output device.

}


Sep 8 '07 #6
Keep in mind if no boxes are checked you don't get the value passed
back. Following are a couple functions I put together to retain my
sanity in checklists and other array input use it like:

$arrayname = postarrayfix('postarrayname');

It strips out any potentially bad HTML and slashes if magic quotes are
on.
/*$_POST Fixer, if the following lists are empty, the POSTs return
nulls, which is bad, as the code needs arrays (even empty) to display,
this fixes it.*/

function postArrayFix($postname){
if( empty($_POST[$postname]) ){
$value = array();
} else {
$value = cleanArray($_POST[$postname]);
}
return $value;
}

/*This one is for if you are reading in arrays from $_POST data, it
will walk deep into the array and clean it up.
*/

function cleanArray($value)
{
if(get_magic_quotes_gpc()) {
$value = is_array($value) ?
array_map('cleanArray', $value) :
stripslashes(strip_tags($value));
} else {
$value = is_array($value) ?
array_map('cleanArray', $value) :
strip_tags($value);
}
return $value;
}

Sep 9 '07 #7
David Gillen wrote:
Jerry Stuckle said:
>Lorenzo Thurman wrote:
>>I have a group of checkboxes drawn by javascript and I need to get each
of their states into PHP after submitting the page. I should think I
could get them in a POST, but that fails. Anyone have any ideas?
TIA
It's how HTML works. Only checked boxes are sent in the GET or POST
request. Unchecked ones are not.
So check isset($_POST['myCheckBox']).

D.
Yep, that will work just fine.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 10 '07 #8

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

Similar topics

2
by: FlashMerlot | last post by:
We have an ASPX (C#) webform which hosts dropdownlists, textboxes, grids, checkboxes, etc. The users want to: #1 - "FREEZE" the entire webpage contents (and state) #2 - move IE browser to...
1
by: Aaron Queenan | last post by:
Is there any way to have tri-state checkboxes in the TreeView, preferably without having to use the state icons for that purpose. Does anyone have or know of an implementation of tri-state...
3
by: Jimmy | last post by:
Hi I have build a datalist with checkboxes in it and after a postback the state of the checkboxes have not been saved. I have enabled the viewstate of all the controls on this page. What do I...
2
by: Pete Moss | last post by:
During a postback event, I am having trouble retrieving the state of a CheckBox Control that I am dynamically adding to a DataGrid Control using ASP.NET 1.1. I have no trouble adding the...
10
by: Peter Olcott | last post by:
Someone told me that determining the exact location and current state of any JavaScript controls is pretty easy. Does anyone know exactly how this is done?
6
by: arun.hallan | last post by:
Hi, I have a datagrid whose datasource is a datatable which is saved to viewstate on page_load. I also have a cached string which handles what is shown on the rowfilter of the dataview of...
1
by: aspnoob | last post by:
hello i just started off with asp.net , needed some help on how to display the state of checkboxes from a SQL database. my database table has the following format seat.dbo ( db design) (...
1
by: Steve Swift | last post by:
Is there anything in HTML today, or perhaps in the near future that would support the tri-state checkboxes that Windows sometimes uses (in the third state, the box is filled with a green "wash") ...
4
by: virk1711 | last post by:
I want to retain the state of checkboxes after the submit button has been clicked. What it is currently doing is that it is resetting the checkboxes after the submit button is clicked. My code is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
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...
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,...

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.