ajax php下拉列表 [英] ajax php drop down list
问题描述
基本上我做的完全一样,在turorial和问题是第二个下拉列表没有显示任何内容。我读了一个有人忘了在页面上添加一些javascript的评论之一。我如何做到这一点?
我已经尝试在该网站上发布问题,但没有人回答一个星期,所以我来到这里。
任何帮助将不胜感激。
这是我的index.php页面
<?php
include('cn.php');
$ sql_country =SELECT * FROM COUNTRY;
$ result_country = mysql_query($ sql_country);
echo< select name ='country'onChange ='get_cities(this.value)'>; // get_cities定义在
之下($ row_country = mysql_fetch_array($ result_country))
{
echo< option value ='$ row_country ['id' ] '> 中$ row_country [。 '国家'。 < /选项> 中;
}
echo< / select>;
echo< select name ='city'id ='city'>< / select>; //我们给了这个下拉列表的id
?>
这是我的get_cities.js页面
函数get_cities(country_id)
{
$ .ajax({
type:POST,
url:cities.php ,/ *国家ID将被发送到这个文件* /
beforeSend:function(){
$(#city)。html(< option> Loading ...< /选项>);
},
data:country_id =+ country_id,
success:function(msg){
$(#city)。 );
}
});
}
这是我的cities.php页面
<?php
include('cn.php');
//代码为cities.php
$ country_id = $ _REQUEST ['country_id'];
$ sql_city =SELECT * FROM CITY WHERE country_id ='。$ country_id。';
$ result_city = mysql_query($ sql_city);
echo< select name ='city'>;
while($ row_city = mysql_fetch_array($ result_city))
{
echo< option value ='$ row_city ['id']。'> < $ row_city [ '城市']。 ; /选项> 中;
}
echo< / select>;
?>
包含的'cn.php'只是我与数据库的连接。
// Index.php
<?php
$ conn = mysql_connect localhost,root,root);
$ db = mysql_select_db(country_example,$ conn);
$ sql_country =SELECT * FROM country;
$ result_country = mysql_query($ sql_country);
?>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< title>国家/地区列表< / title>
< / head>
< body>
<?php
echo< select name ='country'onChange ='get_cities(this.value)'>;
while($ row_country = mysql_fetch_array($ result_country))
{
echo< option value ='$ row_country ['id']。'> < $ row_country [ '国家']。 ; /选项> 中;
}
echo< / select>;
echo< div id ='cityLayer'>< select name ='city'id ='city'>< / select>< / div>;
?>
< script src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\"></script>
< script type =text / javascript>
函数get_cities($ country_id){
$ .ajax({
url:city.php?country_id =+ $ country_id,
cache:false,
beforeSend:function(){
//显示消息
},
完成:function($ response,$ status){
if($ status!=error& ;& $ status!=timeout){
$('#cityLayer')。html($ response.responseText);
}
},
错误:function ($ responseObj){
alert(处理您的请求时发生错误。\\\
\\\
Error =>
+ $ responseObj.responseText);
}
});
}
< / script>
< / body>
< / html>
//City.php
<?php
$ conn = mysql_connect(localhost,root,root);
$ db = mysql_select_db(country_example,$ conn);
$ country_id = $ _REQUEST ['country_id'];
$ sql_city =SELECT * FROM cities WHERE country_id ='。$ country_id。';
$ result_city = mysql_query($ sql_city);
echo< select name ='city'>;
while($ row_city = mysql_fetch_array($ result_city))
{
echo< option value ='$ row_city ['id']。'>$ row_city [ '城市'] < /选项> 中。
}
echo< / select>;
?>
Can some one tell me what's wrong with this example code on this site http://www.x-developer.com/php-scripts/loading-drop-downs-with-ajax-php-and-fetching-values-from-database-without-refreshing-the-page
Basically i did exactly the same as in the turorial and the problem is that the 2nd drop down list is no showing anything. I read one of the comments that someone forgot to add in some javascript on the page. How do i do this?
I have tried posting a question on that site but no one answered for a week now so I came here.
Any help would be much appreciated.
this is my index.php page
<?php
include('cn.php');
$sql_country = "SELECT * FROM COUNTRY";
$result_country = mysql_query($sql_country);
echo "<select name='country' onChange='get_cities(this.value)'>"; //get_cities is defined below
while($row_country = mysql_fetch_array($result_country))
{
echo "<option value='".$row_country['id']."'>".$row_country['country']."</option>";
}
echo "</select>";
echo "<select name='city' id='city'></select>"; //We have given id to this dropdown
?>
this is my get_cities.js page
function get_cities(country_id)
{
$.ajax({
type: "POST",
url: "cities.php", /* The country id will be sent to this file */
beforeSend: function () {
$("#city").html("<option>Loading ...</option>");
},
data: "country_id="+country_id,
success: function(msg){
$("#city").html(msg);
}
});
}
This is my cities.php page
<?php
include('cn.php');
// Code for cities.php
$country_id = $_REQUEST['country_id'];
$sql_city = "SELECT * FROM CITY WHERE country_id = '".$country_id."'";
$result_city = mysql_query($sql_city);
echo "<select name='city'>";
while($row_city = mysql_fetch_array($result_city))
{
echo "<option value='".$row_city['id']."'>".$row_city['city']."</option>";
}
echo "</select>";
?>
The included 'cn.php' is just my connection to the database.
//Index.php
<?php
$conn = mysql_connect("localhost", "root", "root");
$db = mysql_select_db("country_example", $conn);
$sql_country = "SELECT * FROM country";
$result_country = mysql_query($sql_country);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Country List</title>
</head>
<body>
<?php
echo "<select name='country' onChange='get_cities(this.value)'>";
while($row_country = mysql_fetch_array($result_country))
{
echo "<option value='".$row_country['id']."'>".$row_country['country']."</option>";
}
echo "</select>";
echo "<div id='cityLayer'><select name='city' id='city'></select></div>";
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function get_cities($country_id){
$.ajax({
url : "city.php?country_id="+$country_id,
cache : false,
beforeSend : function (){
//Show a message
},
complete : function($response, $status){
if ($status != "error" && $status != "timeout") {
$('#cityLayer').html($response.responseText);
}
},
error : function ($responseObj){
alert("Something went wrong while processing your request.\n\nError => "
+ $responseObj.responseText);
}
});
}
</script>
</body>
</html>
//City.php
<?php
$conn = mysql_connect("localhost", "root", "root");
$db = mysql_select_db("country_example", $conn);
$country_id = $_REQUEST['country_id'];
$sql_city = "SELECT * FROM cities WHERE country_id = '".$country_id."'";
$result_city = mysql_query($sql_city);
echo "<select name='city'>";
while($row_city = mysql_fetch_array($result_city))
{
echo "<option value='".$row_city['id']."'>".$row_city['city']."</option>";
}
echo "</select>";
?>
这篇关于ajax php下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!