我正在尝试创建一个搜索框,其中从'box1'中选择的选项会填充'box2'可用的选项。两个框的选项都来自我的MYSQL数据库。我的问题是我不知道如何基于第一个查询执行查询而不刷新页面,这将是乏味和烦人的。
HTML / PHP
<form role="form" action="search.php" method="GET">
<div class="col-md-3">
<select class="form-control">
<?php
$result = mysqli_query($con,"SELECT `name` FROM school");
while($row = mysqli_fetch_array($result)) {
echo '<option name="'.$row['name'].'">'.$row['name'].' School</option>';
}
?>
</select>
</div>
<div class="col-md-3">
<select class="form-control">
<?php
$result = mysqli_query($con,"SELECT * FROM products");
while($row = mysqli_fetch_array($result)) {
echo '<option name="'.$row['product'].'">'.$row['product'].'</option>';
}
mysqli_close($con);
?>
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>
我认为查询会像这样。 AJAX可能是这个问题的解决方案,但我不确定如何使用AJAX执行此查询而不刷新。
SELECT `product` FROM products WHERE `school` = [SCHOOL NAME FROM BOX 1]
提前致谢!
答案 0 :(得分:6)
首先使用php创建no1选择菜单,如上所述。然后添加一个'change'eventListener,如:
$('#select1').change(createSelect2);
function createSelect2(){
var option = $(this).find(':selected').val(),
dataString = "option="+option;
if(option != '')
{
$.ajax({
type : 'GET',
url : 'http://www.mitilini-trans.gr/demo/test.php',
data : dataString,
dataType : 'JSON',
cache: false,
success : function(data) {
var output = '<option value="">Select Sth</option>';
$.each(data.data, function(i,s){
var newOption = s;
output += '<option value="' + newOption + '">' + newOption + '</option>';
});
$('#select2').empty().append(output);
},
error: function(){
console.log("Ajax failed");
}
});
}
else
{
console.log("You have to select at least sth");
}
}
现在,no2选择菜单根据选择的1选项提供了新选项。
和php文件:
<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
if(isset($_GET['option']))
{
$option = $_GET['option'];
if($option == 1)
{
$data = array('Arsenal', 'Chelsea', 'Liverpool');
}
if($option == 2)
{
$data = array('Bayern', 'Dortmund', 'Gladbach');
}
if($option == 3)
{
$data = array('Aek', 'Panathinaikos', 'Olympiakos');
}
$reply = array('data' => $data, 'error' => false);
}
else
{
$reply = array('error' => true);
}
$json = json_encode($reply);
echo $json;
?>
当然我使用了一些演示数据,但你可以在那里填充一个sql查询填充$ data数组,然后用正确的标题将它们作为json发送。最后在第二个选择菜单中使用更多js:
$('#select2').change(selectSelect2);
function selectSelect2(){
var option = $(this).find(':selected').val();
if(option != '')
{
alert("You selected: "+option);
}
else
{
alert("You have to select at least sth");
}
}
点击这里http://jsfiddle.net/g3Yqq/2/一个工作示例
答案 1 :(得分:0)
试试这个:
You made one mistake here in select && option tag structure of HTML.
Just modify this and your code will work.
<select class="form-control" name="ddlist1">
&&
<?php
echo '<option value = "'.$row['name'].'">'.$row['name'].' School</option>';
?>
>> Add name property in select statement and value in place of name in option tag.
谢谢!
答案 2 :(得分:0)
也许你可以创建一个javascript二维数组,将关系学校保存到产品中。选择学校名称时,您可以按学校名称获取产品列表作为数组中的键,并更新box2的选项列表。
也许你可以像这样回应一个js字符串:
<script language="javascript">
var array = { "school_name1" : { "product1", "poduct2" }, "school_name2", { "product3", "product4" } };
//you can get the product list by array['school_name1'], and you use js to update the product list
</script>
感谢
答案 3 :(得分:0)
你应该试试这个;
的search.php
<?php
// Check if the user wants the school or the school products (based on what the $.getJSON function sends us
if ( ! isset( $_GET['products'] ) && ! empty( $_GET['school'] ) ) {
$sql_schools = mysqli_query( $con, "SELECT `name` FROM school" );
$schools = '';
while ( $school = mysqli_fetch_array( $sql_schools ) ) {
$schools .= '<option value="' . $school['name'] . '">' . $school['name'] . '</option>';
}
}
// The user selected a school and now we will send back a JSON array with products, belonging to the specified school
else {
$school = mysqli_real_escape_string( $_GET['school'] );
$sql_products = mysqli_query( $con, "SELECT `product` FROM products WHERE school = '{$school}'" );
$products = array();
while ( $product = mysqli_fetch_array( $sql_products ) ) {
// Note: If you use array_push() to add one element to the array it's better to use $array[] =
// because in that way there is no overhead of calling a function.
// http://php.net/manual/en/function.array-push.php
$products[] = $product['product'];
}
header( "Content-Type: application/json" );
echo json_encode( $products );
die;
}
?>
<form role="form" action="search.php" method="GET">
<div class="col-md-3">
<select class="form-control" id="schools" name="school">
<?= $schools; ?>
</select>
</div>
<div class="col-md-3">
<select class="form-control">
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
// When #schools gets changed to a new value, load the products belonging to that school
$('#schools').on('change', function() {
$.getJSON('search.php?products&school=' + this.value, function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<option value="' + val + '">' + val + '</option>');
});
$('#schools').empty().append(items);
});
});
</script>