我的网站上有一个页面,列出了我的MySQL数据库中的所有信息,并将其显示在html表中。在我的html表格上方,我还有另一个带有按钮的html表格和一个带有复选框的下拉列表,可用来过滤其中包含数据库信息的表格。我所有的过滤器都是单独工作的,但是当我尝试使它们一起工作时,页面将重新加载,并且仅显示来自最近按下的过滤器的过滤信息。反正是有办法使所有post方法一起工作吗?
以下是我希望它如何工作的示例:
(this parts a
dropdown menu)
[x]loop rooftop happy hour
[ ]river north [yes] [no] [yes] [no]
[ ]old town
[x]river west
[ ]west loop
[submit]
提交后,所有复选框都将发布。这部分有效,但是如果我为屋顶单击“是”,则仅显示屋顶的信息,并且似乎未选中该复选框。
// my filter table
<form method="POST">
<table id="filter">
<div id="title"> <h4> Filters </h4></div>
<tr>
<th> <h4> Rooftop </h4> </th>
<th> <h4> Area </h4> </th>
<th> <h4> Happy Hour </h4> </th>
<th> <h4> Food </h4> </th>
</tr>
<tr>
<td>
<input type="submit" name="rooftop" value="Yes" class="editbtn"/>
<input type="submit" name="noRooftop" value="No" class="editbtn"/>
</td>
<td>
<div id="sortBy">
<button onclick="dropDown()" type="button" class="dropbtn"> ‿ </button>
<div id="myDropdown" class="dropdown-content">
<div id="area">
<input type="checkbox" class="get_value" id="loop" name="loop" value="Loop">Loop<br>
<input type="checkbox" class="get_value" id="Old Town" name="oldTown" value="Old Town"> Old Town <br>
<input type="checkbox" class="get_value" id="River North" name="riverNorth" value="River North"> River North <br>
<input type="checkbox" class="get_value" id="River West" name="riverWest" value="River West"> River West <br>
<input type="checkbox" class="get_value" id="West Loop" name="westLoop" value="West Loop"> West Loop <br>
<input id="areaSubmit" type="submit" value="Search" onclick("submitArea()")>
</div>
</div>
</td>
<td align="center">
<input type="submit" name="happyHour" value="Yes" class="editbtn"/>
<input type="submit" name="noHappyHour" value="No" class="editbtn"/>
</td>
<td>
<input type="submit" name="food" value="Yes" class="editbtn"/>
<input type="submit" name="noFood" value="No" class="editbtn"/>
</td>
</tr>
</table>
// my php that filters the info
<?php
require_once("database_connection.php");
$db = db_connect();
$loop = $_POST["loop"];
$oldTown = $_POST["oldTown"];
$riverNorth = $_POST["riverNorth"];
$riverWest = $_POST["riverWest"];
$westLoop = $_POST["westLoop"];
$rooftop = $_POST["rooftop"];
$noRooftop = $_POST["noRooftop"];
$happyHour = $_POST["happyHour"];
$noHappyHour = $_POST["noHappyHour"];
$food = $_POST["food"];
$noFood = $_POST["noFood"];
$sql = "SELECT bar_name, area, hourStart, hourEnd FROM barInfo WHERE visible = 1";
if(isset($loop)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR area = 'Loop'";
}
else{
$sql .= " AND area = 'Loop'";
}
}
if(isset($oldTown)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR area = 'Old Town'";
}
else{
$sql .= " AND area = 'Old Town'";
} }
if(isset($riverNorth)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR area = 'River North'";
}
else{
$sql .= " AND area = 'River North'";
}
}
if(isset($riverWest)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR area = 'River West'";
}
else{
$sql .= " AND area = 'River West'";
}
}
if(isset($westLoop)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR area = 'West Loop'";
}
else{
$sql .= " AND area = 'West Loop'";
}
}
if(isset($rooftop)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR rooftop = 1";
}
else{
$sql .= " AND rooftop = 1";
}
}
if(isset($noRooftop)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR rooftop = 0";
}
else{
$sql .= " AND rooftop = 0";
}
}
if(isset($happyHour)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR area = 'Loop'";
}
else{
$sql .= " AND area = 'Loop'";
}
}
if(isset($noHappyHour)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR happyHour = 0";
}
else{
$sql .= " AND happyHour = 0";
}
}
if(isset($food)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR food = 1";
}
else{
$sql .= " AND food = 1";
}
}
if(isset($noFood)) {
$sqlArr = explode(" ", $sql);
$count = 0;
foreach($sqlArr as $word){
if ($word == "AND"){
$count += 1;
}
}
if($count > 0){
$sql .= " OR food = 0";
}
else{
$sql .= " AND food = 0";
}
}
$result = mysqli_query($connection, $sql);
?>
// how i post the info as a html table
<?php
if (mysqli_num_rows($result) > 0) {
echo "<table id='list'><tr><th><h3>Name</h3></th><th><h3> Area</h3></th><th><h3>Happy Hour Times</h3></th></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr> <th> <h6> " . $row["bar_name"] . "</h6> </th> <th> <h6>" . $row["area"] . "</h6> </th> <th> <h6>" . $row["hourStart"] . "-" . $row["hourEnd"] . "</h6> </th> </tr>";
}
echo "</table";
}
?>
感谢任何帮助或资源,谢谢!
答案 0 :(得分:0)
首先,我们不需要看到您的SubmitArea函数。
我确实修改,简化并保护了一点代码(不是所有,但是仍然..)您的代码,请注意,我没有对其进行测试。
1)
在您的HTML中,删除您的.editBtn
提交按钮。对于“是/否”选择,我建议您使用一些<select>
下拉菜单或<input type="radio">
按钮。
<form>
<table id="filter">
<div id="title"> <h4> Filters </h4></div>
<tr>
<th> <h4> Rooftop </h4> </th>
<th> <h4> Area </h4> </th>
<th> <h4> Happy Hour </h4> </th>
<th> <h4> Food </h4> </th>
</tr>
<tr>
<td>
<select id="rooftop" class="selectMenu">
<option value="">Select an option</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td>
<div id="sortBy">
<button onclick="dropDown()" type="button" class="dropbtn"> ‿ </button>
<div id="myDropdown" class="dropdown-content">
<div id="area">
<input type="checkbox" class="get_value" id="loop" name="loop" value="Loop">Loop<br>
<input type="checkbox" class="get_value" id="Old Town" name="oldTown" value="Old Town"> Old Town <br>
<input type="checkbox" class="get_value" id="River North" name="riverNorth" value="River North"> River North <br>
<input type="checkbox" class="get_value" id="River West" name="riverWest" value="River West"> River West <br>
<input type="checkbox" class="get_value" id="West Loop" name="westLoop" value="West Loop"> West Loop <br>
<input id="areaSubmit" type="submit" value="Search" onclick("submitArea()")>
</div>
</div>
</td>
<td align="center">
<select id="happyHour" class="selectMenu">
<option value="">Select an option</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td>
<select id="food" class="selectMenu">
<option value="">Select an option</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
</tr>
</table>
</form>
2) 将您的区域发送到数组中,而不是一个一个地发送,这使得循环和构建查询变得更加容易,这是使用jQuery对您的SubmitArea()函数进行的简单重制。
<script>
function submitArea() {
var post_vals = {
params: {
areas: [],
rooftop: 0,
happyHour: 0,
food: 0
}
};
$(".get_value checkbox:checked").each(function() {
post_vals.params.areas.push($(this).val());
});
$(".selectMenu option:selected").each(function() {
if($(this).attr("id") == "rooftop") {
post_vals.params.rooftop = 1;
} else if($(this).attr("id") == "happyHour") {
post_vals.params.happyHour = 1;
} else if($(this).attr("id") == "food") {
post_vals.params.food = 1;
}
});
alert("Check your console for posted value!");
console.log("Post values:");
console.log(post_vals);
$.getJSON("your_php_script.php", {
params: params
}).done(function(data) {
// For now if you look in the console, you'll only see your query..
alert("Post finished, check the response in your console.");
console.log(data);
// Get your data here and do what ever you want with it, IE: rebuild your table rows if they're not already existing.
});
}
</script>
3) 简化您的PHP并通过SQL注入更加安全,请在google或本网站上阅读有关此内容的信息。对于这个例子,我称它为your_php_script.php;)
<?php
require_once("database_connection.php");
$connection = db_connect();
$response = array(
"status" => "success",
"data" => array(), // Table data goes in here
"query" => "" // Temporairy, just for your example to show in your Javascript console
);
if(isset($_GET["params"]) && is_array($_GET["params"])) {
// Prepare mysql statement
$sql = "SELECT bar_name, area, hourStart, hourEnd FROM barInfo WHERE visible = 1";
// Loop over the sent parameters
foreach($_GET["params"] AS $key => $opt) {
if($key == "areas") {
if(is_array($opt)) {
// HERE Beware that you should check the values before you put them in the query for injections security reasons.
foreach($opt AS $k => $area) {
// Real basic method of doing it.. be aware that this is not bullet proof.
$opt[$k] = mysqli_real_escape_string($connection, $area);
}
$sql .= " AND area IN ('" . implode("', '", $opt) . "') ";
}
} else if($key == "rooftop") {
if($opt == 1) {
$sql .= " AND rooftop = 1 ";
} else {
$sql .= " AND rooftop = 0 ";
}
} else if($key == "happyHour") {
if($opt == 1) {
$sql .= " AND happyHour = 1 ";
} else {
$sql .= " AND happyHour = 0 ";
}
} else if($key == "food") {
if($opt == 1) {
$sql .= " AND food = 1 ";
} else {
$sql .= " AND food = 0 ";
}
}
}
$result = mysqli_query($connection, $sql);
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$response["data"][] = $row;
}
// Temporairy
$response["query"] = $sql;
} else {
$response["status"] = "error";
$response["details"] = "MySQL Query error: " . mysqli_error($connection);
}
} else {
$response["status"] = "error";
$response["details"] = "Parameter received is not an array.";
}
echo json_encode($response);
我希望有帮助。
答案 1 :(得分:-1)
您可以按顺序限制结果的顺序,以最后一个顺序排列,最后一个顺序排列。 W3Schools参考:W3Schools SQL - Functions