dounao4179 2013-03-14 16:58
浏览 34
已采纳

ajax php下拉列表[关闭]

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.

  • 写回答

1条回答 默认 最新

  • duanfuxing2212 2013-03-14 17:12
    关注
    //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.
    
    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['city']."</option>";
    }
    echo "</select>";
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址