php - ajax php 下拉列表

标签 php mysql ajax drop-down-menu

有人可以告诉我这个网站上的示例代码有什么问题吗http://www.x-developer.com/php-scripts/loading-drop-downs-with-ajax-php-and-fetching-values-from-database-without-refreshing-the-page

基本上我所做的与图例中的完全相同,问题是第二个下拉列表没有显示任何内容。我读到有人忘记在页面上添加一些 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 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

?>

这是我的 get_cities.js 页面

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);
   }
   });
 } 

这是我的 city.php 页面

<?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>";

?>

包含的“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//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>";
?>

关于php - ajax php 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15415571/

相关文章:

php - 使用 like 语句仅返回出现 x 次的命中

php - '语法错误,意外的 T_CONSTANT_ENCAPSED_STRING' 将 google 事件跟踪添加到 WordPress 插件中的按钮

php - 无法在 PHP/MySQL 中插入 "𤠣"字符

javascript - 可以对文件 ://protocol? 使用react

php - 使用命名空间 PhP 5.5 时访问第 3 方代码

php - Mysql查询基于列匹配

php - 提交多段时出现403错误

mysql 删除仅在文本字段开头的\r\n 吗?

php - 动态和依赖形式

java - dwr反向ajax股票演示应用程序