<bdo id='i8rHj'></bdo><ul id='i8rHj'></ul>
  • <small id='i8rHj'></small><noframes id='i8rHj'>

    <i id='i8rHj'><tr id='i8rHj'><dt id='i8rHj'><q id='i8rHj'><span id='i8rHj'><b id='i8rHj'><form id='i8rHj'><ins id='i8rHj'></ins><ul id='i8rHj'></ul><sub id='i8rHj'></sub></form><legend id='i8rHj'></legend><bdo id='i8rHj'><pre id='i8rHj'><center id='i8rHj'></center></pre></bdo></b><th id='i8rHj'></th></span></q></dt></tr></i><div id='i8rHj'><tfoot id='i8rHj'></tfoot><dl id='i8rHj'><fieldset id='i8rHj'></fieldset></dl></div>
    <tfoot id='i8rHj'></tfoot>

        <legend id='i8rHj'><style id='i8rHj'><dir id='i8rHj'><q id='i8rHj'></q></dir></style></legend>

        ajax php 下拉列表

        时间:2023-10-03

          <tbody id='ooahG'></tbody>

      1. <i id='ooahG'><tr id='ooahG'><dt id='ooahG'><q id='ooahG'><span id='ooahG'><b id='ooahG'><form id='ooahG'><ins id='ooahG'></ins><ul id='ooahG'></ul><sub id='ooahG'></sub></form><legend id='ooahG'></legend><bdo id='ooahG'><pre id='ooahG'><center id='ooahG'></center></pre></bdo></b><th id='ooahG'></th></span></q></dt></tr></i><div id='ooahG'><tfoot id='ooahG'></tfoot><dl id='ooahG'><fieldset id='ooahG'></fieldset></dl></div>
            <bdo id='ooahG'></bdo><ul id='ooahG'></ul>

            <tfoot id='ooahG'></tfoot>

            <small id='ooahG'></small><noframes id='ooahG'>

            <legend id='ooahG'><style id='ooahG'><dir id='ooahG'><q id='ooahG'></q></dir></style></legend>

                  本文介绍了ajax php 下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  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

                  基本上我做的和在 turorial 中完全一样,问题是第二个下拉列表没有显示任何内容.我读到有人忘记在页面上添加一些 javascript 的评论之一.我该怎么做?

                  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.

                  任何帮助将不胜感激.

                  这是我的 index.php 页面

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

                  这是我的 get_cities.js 页面

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

                  这是我的城市.php页面

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

                  包含的cn.php"只是我与数据库的连接.

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

                  这篇关于ajax php 下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何从 PHP 数组中填充列表框/下拉框/选择框 下一篇:如何使用 php 预先选择一个 html 下拉列表?

                  相关文章

                  最新文章

                  <legend id='arhxR'><style id='arhxR'><dir id='arhxR'><q id='arhxR'></q></dir></style></legend>
                  1. <small id='arhxR'></small><noframes id='arhxR'>

                    1. <i id='arhxR'><tr id='arhxR'><dt id='arhxR'><q id='arhxR'><span id='arhxR'><b id='arhxR'><form id='arhxR'><ins id='arhxR'></ins><ul id='arhxR'></ul><sub id='arhxR'></sub></form><legend id='arhxR'></legend><bdo id='arhxR'><pre id='arhxR'><center id='arhxR'></center></pre></bdo></b><th id='arhxR'></th></span></q></dt></tr></i><div id='arhxR'><tfoot id='arhxR'></tfoot><dl id='arhxR'><fieldset id='arhxR'></fieldset></dl></div>
                      <tfoot id='arhxR'></tfoot>

                      • <bdo id='arhxR'></bdo><ul id='arhxR'></ul>