我正在显示MySQL数据库中的表。我想在每列中使用pulldown来过滤表。我想在不使用提交按钮的情况下动态完成。
正如您将从代码和图片中看到的那样,我几乎设法做到这一点,但$_POST
中返回的数据令人困惑。
首先:$_POST[name]
,$_POST[car]
和$_POST[color]
都返回第一个值,无论是否选择了该选择功能。为什么是这样?我已编码(如你所见)
第二:如果我选择了Bob',代码第一次运行,但如果我选择“Bob'再次,$_POST
表现得像空。啊?
我认为基本代码的错误是什么?我在这个网站和互联网上看起来很高和很低,试图找到关于$ _POST如何在这方面发挥作用的更详细的解释。
<?php
//connect to database
$mysqli = mysqli_connect('localhost', 'root', '', "masterlist");
//get the column names from the masterlist table
$get_list_sql = "SHOW COLUMNS FROM listTest";
$get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));
//display_block contains the HTML form data to display
$display_block = "<h1>QLD RNE Design Master List</h1>";
$display_block .="<form action='listTest.php' method='POST'>";
$display_block .="<table><tr>";
//.............................Display field names as header..................................................................
//loop through all the fields, ignore the id field as we don't display it
while ($list = mysqli_fetch_array($get_list_res)) {
if ($list['Field'] <> 'id') {
// simplify the data by using a string to store the current field name
$Field = $list['Field'];
// build select for the field
$display_block .= "<td align = 'center'>".$Field."</td>";
}
}
$display_block .="</tr><tr>";
//.............................Display Sorting options..................................................................
//loop through all the fields, ignore the id field as we don't display it
$get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));
$get_list_sql = "SELECT * from listTest order by name";
while ($list = mysqli_fetch_array($get_list_res))
{
if ($list['Field'] <> 'id')
{
// get the unique list of data in the field to use in the select field for filtering
$SQL = "SELECT DISTINCT ".$list['Field']." from listTest ORDER BY ".$list['Field']." ASC ";
$get_list = mysqli_query($mysqli, $SQL) or die(mysqli_error($mysqli)) ;
// simplify the data by using a string to store the current field name
$Field = $list['Field'];
if(isset($_POST[$Field]) && $_POST[$Field] <> $Field)
{
echo($Field.' should have been pressed and has a value of '.$_POST[$Field].'<br>');
$get_list_sql = "SELECT * from listTest where ".$Field." ='" .$_POST[$Field]. "' order by name";
$name = $_POST[$Field];
}else
{
$name = $Field;
}
echo ('name = '.$name.'<br>');
// build select for the field
$display_block .= "<td align = 'center'><select name ='".$name."' id='".$Field."' onchange='this.form.submit()'>";
//set the first option to the field name
$display_block .= "<option value=".$Field.">".$Field."</option>";
//loop through the data list
while ($select = mysqli_fetch_array($get_list))
{
if ($select[$Field]<>null)
{
$display_block .= "<option value=".$select[$Field].">".$select[$Field]."</option>";
}
}
$display_block .="</select></td>";
}
}
//.............................Display Data..................................................................
foreach ($_POST as $key => $value)
{
echo "<pre><tr>";
echo "<td> ";
echo $key;
echo "</td>";
echo "<td> ";
echo $value;
echo "</td>";
echo "</tr></pre>";
}
echo($get_list_sql );
$get_list_res = mysqli_query($mysqli, $get_list_sql) or die(mysqli_error($mysqli));
while ($list = mysqli_fetch_array($get_list_res))
{
$display_block .= "<tr><td>".$list['name']."
</td><td>".$list['color']."
</td><td>".$list['car']."
</td>";
}
//echo('finished');
$display_block .="</tr>";
$display_block .="</form></table>";
//free results
mysqli_free_result($get_list_res);
//close connection to MySQL
mysqli_close($mysqli);
//$_POST = array();
//.............................HTML.................................................................
?>
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="w3.css">
<head>
<style>
h1 {
border-bottom: 3px solid #cc9900;
color: #996600;
font-size: 20px;
}
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
font-size: 15px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
由于我只能粘贴12个链接,这里是运行代码的一个例子。拳头图像是开始,第二个是选择&#39; bob&#39;第三是选择&#39; bob&#39;第二次:
我希望有人可以提供帮助,请不要建议我用其他语言写这个。我希望它在PHP中。我使用Chrome作为网络浏览器