我有这段代码,我从中打印表格行和一个复选框,我需要在另一个 PHP 文件中打印选中的行。 我应该怎么做?
我需要像 sql=select $checkbox 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); // 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>
好的。因此,首先您应该只让此文件生成表单。根据表格的布局方式,您希望有 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>
然后在您要提交表单的文件中(报告.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>';
}?>
我希望这足以让你滚球。尝试一下,运行你的代码,看看它是如何表现的。确保您的表单以您想要的方式显示,并且提交正确运行,并且至少向您显示一些内容。如果需要,只需使用我的示例代码,如果您看到某些内容,则表示它到达了那里。然后,您可以将该示例代码替换为您真正想要显示的内容。如果比这更多,我基本上会为你编写代码。试一试。祝你好运。