【发布时间】:2017-12-07 08:25:50
【问题描述】:
我有两个下拉选择选项。第一个是选择州,第二个是选择城市。当任何一个从州下拉列表中选择时,相关内容应显示在第二个选择选项(将是相关城市)上。我有一些代码,但它不起作用。
<div class="vali-form">
<div class="col-md-6 form-group2">
<?php
include("connection.php");
$query1=mysql_query("select * from location_master where parent_id='0' ORDER BY location") or die (mysql_error());
echo" <label class='control-label'>Select State</label>";
echo" <select required name='selectstate'>";
echo"<option>Select</option>";
while($row1=mysql_fetch_array($query1))
{
echo"<option value='".$row1['id']."'>".$row1['location']."</option>";
}
echo" </select>";
?>
</div>
<div class="col-md-6 form-group2" id="response">
<label class='control-label'>City:</label>
<select>
<?php
if(isset($_POST["selectstate"])){
// Capture selected country
$selectstate = $_POST["selectstate"];
// Define country and city array
$query1=mysql_query("select * from location_master where id='$selectstate'") or die (mysql_error());
$row1=mysql_fetch_array($query1);
// Display city dropdown based on country name
if($selectstate !== 'Select'){
foreach($row1[$selectstate] as $value){
echo "<option>". $value . "</option>";
}
}
}
?>
</select>
</div>
<div class="clearfix"> </div>
</div>
jQuery-AJAX 代码如下。
<script type="text/javascript" src="http://code.jquery.com/jquery.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("select.selectstate").change(function(){
var selectedstate = $(".selectstate option:selected").val();
$.ajax({
type: "POST",
url: "locpac.php",
data: { selectstate : selectedstate }
}).done(function(data){
$("#response").html(data);
});
});
});
</script>
【问题讨论】:
-
如果您在 2018 年仍在使用 PHP 已弃用的 mysql_ API,那么坦率地说没有希望
标签: javascript php jquery mysql ajax