dsxay48646 2014-10-30 12:50
浏览 20
已采纳

自动加载下拉菜单值

i have a problem, I have 3 drop down menus, and all of them are connected, i am creating a system that would automatically display the city's zipcode based on the provice and city of the user.

For example, the user chooses province A, then here's my problem, how would i make it so that when the user chooses province A, the system would display all the cities inside province A? i already have a complete database of the zipcode of my country, and after choosing the city, the zipcode will automatically be displayed in a text box that should not be editable but not disabled, disabling a text box would disregard it's content as i remember,

here is my query for the province,

$query = 'SELECT DISTINCT major_area FROM zipcodes GROUP BY major_area ORDER BY major_area asc ';
                        $result = mysql_query($query) or die(mysql_error());
                        echo "<select name='categories' required>";
                        echo "<option value =''>Select Province</option>";
                        while ($row = mysql_fetch_array($result)){
                          $major_area = $row['major_area'];
                          echo "<option value='$major_area'>$major_area</option>";
                        }

                        echo "</select>";

here is my query for the city drop down and major_area is shown with an example with entry 'ABRA'

$query = "SELECT DISTINCT city FROM zipcodes WHERE major_area='ABRA' ORDER BY city asc";
                        $result = mysql_query($query) or die(mysql_error());
                        echo "<select name='categories' required>";
                        echo "<option value =''>Select City</option>";
                        while ($row = mysql_fetch_array($result)){
                          $city = $row['city'];
                          echo "<option value='$city'>$city</option>";
                        }

                        echo "</select>";

and of course the zip code. i havent done it yet, but i know how to query it. i just need to make it automatic but i dont know how.

all help would be appreciated, thank you in advance.

  • 写回答

2条回答 默认 最新

  • douqiao6015 2014-10-30 13:08
    关注

    Here you can find a very easy to understand tutorial on how to do this.

    First of all this is not something that you can do with strictly PHP. You need to refresh the page in order for the other dropdown to populate. From here, we draw the conclusion that you HAVE to use Ajax. Or JQuery .ajax() for that matter. Whatever you feel more comfortable using.

    Here are the steps:

    1. when the first dropdown gets selected you will have an onClick event on it, that will load a JS function

    2. the JS function will have an AJAX call to a php file containing the code for the second dropdown. Of course, you will have to pass the province ID to the JS function and forward to the php file.

    3. on the PHP file, you will have the select from DB with the proper information, selected, you guessed, based on the ID that you sent him. Useful for the WHERE clause, right?

    4. the last PHP function displays the dropdown and there you go :)

    Hope this helps! Read the documentation that I've provided, it WILL help you and keep in mind: You will not learn ANYTHING if you just ask for people on SO to provide you with the ready-code. The best thing you can do FOR YOU is to read the documentations so that the next time you encounter this issue, you will know how to solve it yourself, and not go back to SO to search for the answer or post a duplicate one ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度