Page 1 of 1

Checkboxes in PHP

Posted: Tue Sep 02, 2003 4:01 pm
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.

Posted: Tue Sep 02, 2003 4:17 pm
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

Posted: Tue Sep 02, 2003 4:23 pm
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: ] --

Posted: Tue Sep 02, 2003 4:32 pm
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?

Posted: Tue Sep 02, 2003 4:42 pm
by Stoker
$memberlis = implode (', ',$members);

will join all array items with a , inbetween...

Posted: Tue Sep 02, 2003 6:17 pm
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>

Posted: Tue Sep 02, 2003 7:54 pm
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.