Form Checkboxes!!!

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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Form Checkboxes!!!

Post by Jade »

Hey there,

I run an internet game (http://www.whiteoaks.idlegames.net) and in it my members have the option to buy clothing. Now I want to give my members the option to sell their clothing. I'm trying to accomplish this by selecting the clothing the members use and assigning the row the item is in the database to the value of the checkbox. Then what happens is I use a while loop to gather which checkboxes have been selected...only I'm having some problems with this. Any suggestions on how to pick up which checkboxes are being checked using php in a while loop? What i have so far is below:


Page 1

//get member's clothes

$number = 0;

//statement that selects clothing name using $number

?>
<tr>
<td height="18"></td>
<td colspan="2" valign="top"><font size=2>
<?php

while ($name != "")
{

?>
<tr>
<td height="18"></td>
<td colspan="2" valign="top"><font size=2>
<input type="checkbox" name="<?php echo "name$number"; ?>" value="<?php echo $number; ?>"><br></font></td>
<td></td>
</tr>
<?php

$number++;

//statement that selects clothing name using $number

}

?>

Page 2 (when form is submitted)

while ($_POST['name'.$number] == $number)
{

//DO ALL CALCULATIONS HERE


//statement that selects clothing name using $number

echo "<font size=2>$name";

$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;

if ($_POST['name'.$number] == "")
{
$number++;


}
}
}
}
}
}
}
}
}
}
}
}
}

I know this looks really bad!!! Can anyone help me!!!
Jade
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try this example

Code: Select all

<html><body>
previously selected:
<?php 
	if (isset($_POST['chk']))
	{
		// one method
		echo join(',', $_POST['chk']);
		
		// another method
		echo '<ul>';	
		foreach($_POST['chk'] as $c)
			echo '<li>', $c, '</li>';
		echo '</ul>';
		
		// many more ways
		// ...
	}
?>
<hr />
<form method="POST">
<?php
	for ($i=0; $i!=10; $i++)
		echo '<input type="checkbox" name="chk[]" value="',$i,'" />', $i, '<br />';
?>
<input type="submit" />
</form>
</body></html>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Thanks billions! It works.
Post Reply