php ajax 下拉列表
Posted
技术标签:
【中文标题】php ajax 下拉列表【英文标题】:php ajax droplist 【发布时间】:2013-02-15 07:34:50 【问题描述】:我正在创建 2 个下拉列表,第二个是基于第一个下拉列表的选择。数据取自mysql数据库
索引.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playing With Select list</title>
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300,700' rel='stylesheet' type='text/css' />
<!--[if lte IE 8]><style>.maindisplay:none; .support-note .note-iedisplay:block;</style><![endif]-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
$(".country").change(function()
var id=$(this).val();
var dataString = 'id='+ id;
$.ajax
(
type: "POST",
url: "ajax_category.php",
data: dataString,
cache: false,
success: function(html)
$(".governorate").html(html);
);
);
);
</script>
</head>
<body>
<div class="container">
<header>
<h1><strong>Playing With Select List</strong></h1>
<h2>Select One List To see Output On Other</h2>
</header>
</div>
<span style="margin-left:22%">
<label>country :</label> <select name="country" class="category">
<option selected="selected">--Select Country--</option>
<?php
include('db.php');
$sql=mysql_query("select country_id,country_name from country");
while($row=mysql_fetch_array($sql))
$id=$row['country_id'];
$data=$row['country_name'];
echo '<option value="'.$id.'">'.$data.'</option>';
?>
</select>
<label>Governorate :</label> <select name="governorate" class="subcategory">
<option selected="selected">--Select governorate--</option>
</select>
</span>
<br><br><br>
<h1><center><strong>Go To-:<a href="www.tricktodesign.com">TrickToDesign</a></strong></center></h1>
</body>
</html>
ajax_category.php
<?php
include('db.php');
if($_POST['governorate_id'])
$id=$_POST['governorate_id'];
$sql=mysql_query("select b.governorate_id,b.governorate_name from governorate a,contry_id b where b.country_id=a.country_id and parent='$id'");
while($row=mysql_fetch_array($sql))
$id=$row['governorate_id'];
$data=$row['governorate_name'];
echo '<option value="'.$id.'">'.$data.'</option>';
?>
如何使第二个下拉列表显示其中的数据,这是我面临的错误
【问题讨论】:
【参考方案1】:将 $(".governorate").html(html) 更改为 $(".subcategory").html(html)
【讨论】:
没有 MIIB 我在省而不是子类别,但数据没有出现如何解决它你能帮我吗??以上是关于php ajax 下拉列表的主要内容,如果未能解决你的问题,请参考以下文章
php - Laravel:如何在从下拉列表中选择更改后加载 Ajax 数据?