[英]populating three follow up select lists using php+mysql and jquery ajax post
好的,我已經鏈接了腳本,現在這是索引文件的代碼:
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("select[name='brand']").change(function(){
var brandValue = jQuery("select[name='brand']").val();
jQuery.ajax({
type: "POST",
url: "handler.php",
data: ({brand: brandValue, status: 1}),
beforeSend: function(){ jQuery("#loader").show(); },
complete: function(){ jQuery("#loader").hide(); },
success: function(response){
jQuery("#results").html(response);
jQuery("#results").show();
}
});
});
jQuery('body').on('change','select[name="series"]',function(){
var seriesValue = jQuery("select[name='series']").val();
var brandValue = jQuery("select[name='brand']").val();
jQuery.ajax({
type: "POST",
url: "handler.php",
data: ({series: seriesValue, brand: brandValue, status: 1}),
beforeSend: function(){ jQuery("#loader").show(); },
complete: function(){ jQuery("#loader").hide(); },
success: function(response){
jQuery("#results").html(response);
jQuery("#results").show();
}
});
});
jQuery('body').on('change','select[name="models"]',function(){
var modelsValue = jQuery("select[name='models']").val();
var seriesValue = jQuery("select[name='series']").val();
var brandValue = jQuery("select[name='brand']").val();
jQuery.ajax({
type: "POST",
url: "handler.php",
data: ({models: modelsValue, series: seriesValue, brand: brandValue, status: 1}),
beforeSend: function(){ jQuery("#loader").show(); },
complete: function(){ jQuery("#loader").hide(); },
success: function(response){
jQuery("#results").html(response);
jQuery("#results").show();
}
});
});
});
</script>
Brands:
<select name="brand">
<option value="">Please Select</option>
<?php
$brands = get_brands();
foreach($brands as $brand) {
?>
<option value="<?php echo $brand['brand_id']?>"><?php echo $brand['brand_name']; ?></option>
<?php
}
?>
</select>
<div id="results" style="display:none;"></div>
<div id="loader" style="display:none;"><img src="ajax-loader.gif" alt="loading..."></div>
這是處理文件:
<?php
if(!empty($_POST['brand'])) {
$curentSeries = get_series($_POST['brand']);
?>
Series:
<select name="series">
<option value="">Please Select</option>
<?php
foreach($curentSeries as $ser) {
?>
<option value="<?php echo $ser['series_id']; ?>"><?php echo $ser['series_name']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
<?php
if(!empty($_POST['series'])) {
$curentModels = get_models($_POST['brand'], $_POST['series']);
?>
Model:
<select name="models">
<option value="">Please Select</option>
<?php
foreach($curentModels as $model) {
?>
<option value="<?php echo $model['model_id']; ?>"><?php echo $model['model_name']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
<?php
if(!empty($_POST['models'])) {
echo "<br />brand: {$_POST['brand']}<br />series: {$_POST['series']}<br />model: {$_POST['models']}";
}
腳本現在可以正常工作了,我們弄清楚了,最后的問題在處理文件中,正如我整理出來的那樣,現在一切都很好
由於您的``系列''數據是動態創建的,因此您需要在jquery中使用``on''事件。檢查以下代碼
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("select[name='brand']").change(function(){
var optionValue = jQuery("select[name='brand']").val();
jQuery.ajax({
type: "POST",
url: "data.php",
data: ({brand: optionValue, status: 1}),
beforeSend: function(){ jQuery("#loader").show(); },
complete: function(){ jQuery("#loader").hide(); },
success: function(response){
jQuery("#series").html(response);
jQuery("#series").show();
}
});
});
jQuery('body').on('change','select[name="series"]',function(){
var seriesValue = jQuery("select[name='series']").val();
jQuery.ajax({
type: "POST",
url: "data.php",
data: ({series: seriesValue, status: 1}),
beforeSend: function(){ jQuery("#loader").show(); },
complete: function(){ jQuery("#loader").hide(); },
success: function(response){
jQuery("#model").html(response);
jQuery("#model").show();
}
});
});
});
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.