handling multicheckbox with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

handling multicheckbox with php

Post 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
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

use isset() function to check the chackbox.
its will return true if the box is checked and false other wise.


Mannan
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
Post Reply