处理使用while循环填充的表单,为每个div分配一个复选框

Here is the code from my submit.php file that has the form mentioned in the title. The if and while statements work and all of this code works the way it looks.

<form action="file.php" method="post">
<?php //database connection stuff
...
if($result->num_rows > 0){
echo '<div class="pure-g">';

               while($row = $result->fetch_assoc()){

                echo '<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
                <fieldset id="' . $row[Name] . '">
                    <h3 class="content-subhead">'
                . $row[Name] . '
            </h3>
            <img src="' . $row[Picture] . '" width="100" height="100">                
            <input type="checkbox" name="check" id="check' . $row[Name] . '" value="' . $row[Name] . ',">
            </fieldset>
            </div>';

               }
echo '</div>';
}
else{
     echo "0 results";
}

?>

        </div>
        <input type="submit" value="Add Selected Games">
    </form>

The problem I'm getting is when I use the submit.php:

$conn = mysql_connect($host, $name, $pass);

if(!$conn){
    echo "cannot connect to server" . mysql_error();
}

$select = mysql_select_db($db);

if(!$select){
    echo "cannot select db" . mysql_error();
}

$list = $_REQUEST['check'];

echo " success $list";

There are currently 3 checkboxes showing on the file.php page that will list the fieldset objects. How should I name the checkbox to have it echo the right $row[Name]? It will echo one $row[Name] based on the a checkbox being checked and newest checkbox (created later in the while loop).

When you just want to see an unformatted list or an array you must convert the array to text.

$list = var_export($_REQUEST,false);
echo "sucess $list";

The checkbox value should "1"
The checkbox name should include the identifier

<input type="checkbox" name="check' .$row[Name] . '" id="check' .$row[Name] . '" value="1">

When the form is submitted you get the check value like this:

$list = "<ul>
";
foreach $_POST as $key => $val){   
  if (substr($key,0,5)=='check'){
     $list .= "
<li>" . substr(($key,5);
   } 
 }
 echo "sucess
$list
</ul>";

The Browser only posts the check boxes that are checked. Some Browsers do not pass the Value. The value that is passed with the checkbox key name varies from Browser to Browser.

I also cleaned up the form routine:

<?php 
header('Content-Type: text/html; charset=utf-8');
header('Connection: Keep-Alive');
header('Keep-Alive: timeout=50, max=100');
header('Cache-Control: max-age=120');
echo <<<EOT
<!DOCTYPE HTML>
<html lang="en"><head><title>Title</title><style type="text/css"></style></head><body><div>
<form action="file.php" method="post">
EOT;

if($result->num_rows > 0){
  echo '<div class="pure-g">';

  while($row = $result->fetch_assoc()){
    $name=$row[`Name`];
    $pic = $row[`Picture`];
    echo <<<EOT
<div class="l-box pure-u-1 pure-u-md-1-2 pure-u-lg-1-5">
<fieldset id="$name">
<h3 class="content-subhead">$name</h3>
<img src="$pic" width="100" height="100">                
<input type="checkbox" name="check$name" id="check$name" value="1">
/fieldset>
</div>

EOT;
  }
echo '</div>';
}
else{
  echo '<p>0 results</p>';
}
echo '</div><input type="submit" value="Add Selected Games"></form></body></html>';
?>