我的数据库中有3列为 bodybuild , ethnictype 和 skincolor 表帐户。我需要一次搜索所有三列,使用复选框从中获取值。
<h4 style="margin-left: 10px; color: gray;">Body Build Types</h4><ol><input type="checkbox" name="bodybuild[]" value="1" />Six Pack<br><input type="checkbox" name="bodybuild[]" value="2" />Fat<br><input type="checkbox" name="bodybuild[]" value="3" />Thin<br></ol><h4 style="margin-left: 10px; color: gray;">Ethnic Types</h4><ol><input type="checkbox" name="ethnictype[]" value="4" />Arab<br><input type="checkbox" name="ethnictype[]" value="5" />Indian<br></ol><h4 style="margin-left: 10px; color: gray;">Skin Color Types</h4><ol><input type="checkbox" name="skincolor[]" value="6" />Black<br><input type="checkbox" name="skincolor[]" value="7" />White<br></ol><input style="margin-left: 10px" type="submit" value='Search' /><br><br><input type="hidden" name="tab1" value="search"></form>
列 bodybuild 包含3个值,分别为1,2和3
列 ethnictype 包含2个值,分别为4和5
Colum skincolor 包含2个值,分别为6和7
我想将视图页面中复选框中选择的值传递给像这样的URL
/ search / 1&amp; 2&amp; 3&amp; 4&amp; 5&amp; 6&amp; 7&amp; 7 - 选中所有复选框后
/ search / 1&amp; 4&amp; 6 - 选择三个值(1,4,6)或任何其他方式时。
我可以从表中获取单个值( / search / 1 ),但不能获取多个值。
请帮忙。谢谢!!!
答案 0 :(得分:0)
最后在 AJAX 的帮助下,我得到了我的问题的解决方案。感谢所有支持我的人。我发布了更多候选人的代码。
<强>的index.php 强>
<body>
<h1>Demo</h1>
<table id="employees"><thead>
<tr><th>Name</th>
<th>BodyBuild</th>
<th>EthnicType</th>
<th>Skincolor</th></tr>
</thead><tbody></tbody>
</table>
<div id="filter">
<h2>Filter options</h2>
<div>
<h4>Body Build</h4>
<input type="checkbox" id="car" name="sixpack">
<label for="car">Six Pack</label>
</div>
<div>
<input type="checkbox" id="car" name="fat">
<label for="car">Fat</label>
</div>
<div>
<input type="checkbox" id="car" name="thin">
<label for="car">Thin</label>
</div>
<div>
<h4>Ethnic Type</h4>
<input type="checkbox" id="language" name="arab">
<label for="language">Arab</label>
</div>
<div>
<input type="checkbox" id="language" name="indian">
<label for="language">Indian</label>
</div>
<div>
<h4>Skin Color</h4>
<input type="checkbox" id="nights" name="black">
<label for="nights">Black</label>
</div>
<div>
<input type="checkbox" id="nights" name="white">
<label for="nights">White</label>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k , v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}
function getEmployeeFilterOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.name);
}
});
return opts;
}
function updateEmployees(opts){
$.ajax({
type: "POST",
url: "search",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(records){
$('#employees tbody').html(makeTable(records));
}
});
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getEmployeeFilterOptions();
updateEmployees(opts);
});
updateEmployees();
</script>
</body>
<强>的search.php 强>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=castingkall', 'root', '');
$select = 'SELECT name,bodybuild,ethnictype,skincolor';
$from = ' FROM accounts';
$where = ' WHERE TRUE';
$opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
if (in_array("sixpack", $opts)){
$where .= " AND bodybuild = 1";
}
if (in_array("fat", $opts)){
$where .= " AND bodybuild = 2";
}
if (in_array("thin", $opts)){
$where .= " AND bodybuild = 3";
}
if (in_array("arab", $opts)){
$where .= " AND ethnictype = 4";
}
if (in_array("indian", $opts)){
$where .= " AND ethnictype = 5";
}
if (in_array("black", $opts)){
$where .= " AND skincolor = 6";
}
if (in_array("white", $opts)){
$where .= " AND skincolor = 7";
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>
只需根据您的数据更改数据库名称,表名称,列名称和值。
希望你找到这个有用的