我有这个代码,我打印表行和一个复选框,我需要在另一个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>
答案 0 :(得分: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>';
}?>
我希望这足以让你为球而战。尝试一下,运行代码并查看其行为。确保您的表单以您希望的方式显示,并且提交正确运行并至少向您显示一些内容。如果需要,只需使用我的示例代码,如果你看到它意味着它到达那里。然后,您可以用您真正想要显示的内容替换该示例代码。不仅如此,我基本上都会为您编写代码。试一试。祝你好运。