Page 1 of 1

handling multicheckbox with php

Posted: Wed Nov 30, 2005 12:19 am
by jaylin
i want to know the value of the checkbox which are checked and put theses values to cookie which is like a shopping card.

my problem is how can i know which checkbox are checked or not by php (it works fine in javascript).

plz

Posted: Wed Nov 30, 2005 12:43 am
by itsmani1
use isset() function to check the chackbox.
its will return true if the box is checked and false other wise.


Mannan

Posted: Wed Nov 30, 2005 1:34 am
by jaylin
the checkbox are allocated on the form dynamically accordingly to the database record. So, how do i know how many checkboxes are there and how can i access these?
itsmani1 wrote:use isset() function to check the chackbox.
its will return true if the box is checked and false other wise.


Mannan

Posted: Wed Nov 30, 2005 1:47 am
by jmut
in html

Code: Select all

<input type="checkbox" name="storage[id1_or_something_to_distinguish_them_with]" value="Y">
<input type="checkbox" name="storage[id2_or_something_to_distinguish_them_with]" value="Y">
........
in php

Code: Select all

$checked_checkboxes = $_POST['storage'];  // array with only checked checkboxes will be stored in here.
I hope I understood your question right.

Posted: Wed Nov 30, 2005 2:05 am
by jaylin
it does work but it doesn't display the value of checkbox

Code: Select all

<?php 
	if (isset($_REQUEST["action"]) && $_REQUEST["action"]='submitted')
	{
		$mycheck = $_POST['chk'];	
		foreach ($mycheck as $selectedcar)
		{
			if (isset($mycheck))
			{
				echo "$mycheck<br>";
			}
		}
	}
?>
plz tell me the way
regard,
jmut wrote:in html

Code: Select all

<input type="checkbox" name="storage[id1_or_something_to_distinguish_them_with]" value="Y">
<input type="checkbox" name="storage[id2_or_something_to_distinguish_them_with]" value="Y">
........
in php

Code: Select all

$checked_checkboxes = $_POST['storage'];  // array with only checked checkboxes will be stored in here.
I hope I understood your question right.

Posted: Wed Nov 30, 2005 2:17 am
by jmut
jaylin wrote:it does work but it doesn't display the value of checkbox

Code: Select all

<?php 
	if (isset($_REQUEST["action"]) && $_REQUEST["action"]='submitted')
	{
		$mycheck = $_POST['chk'];	
		foreach ($mycheck as $selectedcar)
		{
			if (isset($mycheck))
			{
				echo "$mycheck<br>";
			}
		}
	}
?>
plz tell me the way
regard,
:)
Your foreach is wrong... you never used $selectedcar
just dump
var_dump($myckeck); // and see if the array have the content you expect.

probably should be ok ittarating with

Code: Select all

foreach ($mycheck as $id => $checkbox_value) {
   //you don't need isset.........because it is only the checked boxes
   echo    $checkbox_values['id1_or_something_to_distinguish_them_with'];
}
If the question is how to itterate arrays....let me know.