-3

我有这段代码,我从中打印表格行和一个复选框,我需要在另一个 php 文件中打印选中的行。我应该怎么做?

我需要 sql=select $checkbox1, checkbox2 之类的东西,或者最好这样做....

<form action='report.php' method='post'>

<?php // Script 12.7 - sopping.php

$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);

echo "<table border='1' class='tabtext'>";

$result = mysql_query("SELECT * FROM hostess");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);

// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
    $field_name = mysql_field_name($result, $field); // instead of $i
    echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}

echo '</tr></thead>';

echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
    $data = mysql_fetch_assoc($result);
    echo '<tr>';
    for ($field = 0; $field < $numfields; $field++) {
        $field_name = mysql_field_name($result, $field);
        if (isset($_POST['checkbox'][$field_name])) {
            echo '<td>' . $data[$field_name] . '</td>';
        }
    }
    echo '</tr>';
}
echo '</tbody>';
echo '</table>';


?>
<input type='submit' value='Submit' />
</form>
4

1 回答 1

1

好的。所以首先你应该只让这个文件生成表单。根据您布置表格的方式,您希望有 2 行,第一行是字段名称,第二行包含复选框本身。所以这就是:

<form action='report.php' method='post'>
<?php // Script 12.7 - sopping.php

$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);

echo "<table border='1' class='tabtext'>";

$result = mysql_query("SELECT * FROM hostess");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);

// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
    $field_name = mysql_field_name($result, $field);
    echo '<th>'. $field_name . '</th>'; // only the field name
}    
echo '</tr></thead>';
echo '<tbody><tr>';
for ($field = 0; $field < $numfields; $field++) {
    $field_name = mysql_field_name($result, $field);
    echo 
        '<td>
             <input type="checkbox" name="checkbox['.$field_name.']" value="1"/>
        </td>';
}
echo '</tr></tbody>';
echo '</table>';
?>
<input type='submit' value='Submit' />
</form>

然后在您提交表单的文件(report.php)中,您将捕获您发布的内容并显示一个仅显示您提交的复选框的新表格。这是您可以执行的操作的示例。

<?php
// within report.php (THIS IS AN EXAMPLE ONLY)

// check if the checkbox fields were submitted
// and if not empty we know that items have been checked.
if(isset($_POST['checkbox']) && !empty($_POST['checkbox'])){
  // iterate through the checked items.
  // this is an associative array because you gave the items a key
  foreach($_POST['checkbox'] as $field => $value){
    // do some stuff
    echo "<p>Checked Field: $field<br/>Value:$value</br></p>";
  }
} else {
  // display a message saying that nothing was submitted
  // you could also display some error or redirect back to the form etc.
  echo '<p>No Check boxes have been checked</p>';
}?>

我希望这足以让你顺利进行。试一试,运行你的代码,看看它的表现如何。确保您的表单以您想要的方式显示并且提交正确运行并且至少向您展示了一些东西。如果需要,只需使用我的示例代码,如果您看到某些内容,则表示它已到达那里。然后,您可以将该示例代码替换为您真正想要显示的内容。除此之外,我基本上会为您编写代码。试一试。祝你好运。

于 2012-06-18T14:49:00.760 回答