Checkboxes in 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
mmahoney
Forum Newbie
Posts: 3
Joined: Tue Sep 02, 2003 4:01 pm

Checkboxes in PHP

Post by mmahoney »

I want to process a list of checkbox values where the name is the same for all of them. I present a list of people who can be selected, and each checkbox is named members. The idea is to check the names of the members for which action should be taken. In the PHP script that receives the data, I try to use $memberlist = $_POST['members']; but I can only see one member. I know that the browser is sending the list, because I can parse the STDIN list in Perl and see them all there. There are multiple arguments that are members=something. How do I process these in PHP?

Thanks.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Name the checkboxes members[].

Then a different value for each check box.

Then, when the form is processed, all the values will be available in an array called $members.


I think anyway :o)

Mark
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

PHP parses the post/get and assigns it all to 'members' so only the last one is there, if you want them in an array you can try to change the name of that field to name="members[]" , I believe that should work..

-- [ and I was late again :roll: ] --
mmahoney
Forum Newbie
Posts: 3
Joined: Tue Sep 02, 2003 4:01 pm

Post by mmahoney »

I put in the processing script $memberlist = $_POST['members'] and an echo of $memberlist is the word Array. How to I get $memberlist to be equal to the values?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

$memberlis = implode (', ',$members);

will join all array items with a , inbetween...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

just as an addition:
$_POST['members'] will be an array containing all checked values.
try

Code: Select all

<html>
	<body>
<?php	if(isset($_POST['member'])) { ?>
		<fieldset><legend>selected member-checkboxes</legend>
			<pre><?php print_r($_POST['member']); ?></pre>
			number of elements in the array: <?php echo count($_POST['member']); ?> <br />
			the first element is: <?php echo $_POST['member'][0]; ?> <br />
			all elements
			<pre><?php
				foreach($_POST['member'] as $index=>$member)
					echo $index, '=>', $member, "\n";
			?></pre>
			the imploded string: <?php echo implode(',', $_POST['member']); ?>
		</fieldset>
<?php } ?>

		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<input type="checkbox" name="member[]" value="1" id="m1" /><label for="m1">member #1</label><br />
			<input type="checkbox" name="member[]" value="2" id="m2" /><label for="m2">member #2</label><br />
			<input type="checkbox" name="member[]" value="3" id="m3" /><label for="m3">member #3</label><br />
			<input type="checkbox" name="member[]" value="4" id="m4" /><label for="m4">member #4</label><br />
			<input type="submit" />
		</form>
	</body>
</html>
mmahoney
Forum Newbie
Posts: 3
Joined: Tue Sep 02, 2003 4:01 pm

Post by mmahoney »

Your reply is very useful; I pasted portions of it into my script and I can now see a solution to my problem. The array is visible, and I think I can figure out how to accomplish what I wanted.

Thanks.
Post Reply