根据第一选择列表值填充第二选择列表 [英] Populate second select list based on first select list value

查看:58
本文介绍了根据第一选择列表值填充第二选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个不起作用的嵌套选择列表.第二个选择列表的值未填充.这是我的代码.我填充第一个选择框,并基于其值getter.php被动态调用以填充第二个选择框(不起作用)

I'm trying to do a nested select list which is not working The values of the second select list dont get populated. Here is my code. I populate the first select and based on its value getter.php is called dynamically to populate the second select box (which is not working)

<div class="col-sm-3">
    <label for="rg">Region</label>
</div>
<div class="col-sm-9">
    <select id="region" name="region">
        <option value="">Select Region</option>
        <option value="AR">Ashanti</option>
        <option value="BRONG+AHAFO">Brong Ahafo</option>
        <option value="CENTRAL">Central</option>
        <option value="EASTERN">Eastern</option>
        <option value="GREATER+ACCRA">Greater Accra</option>
        <option value="NORTHERN">Northern</option>
        <option value="UPPER+EAST">Upper East</option>
        <option value="UPPER+WEST">Upper West</option>
        <option value="VOLTA">Volta</option>
        <option value="WESTERN">Western</option>
    </select>
</div>

<div class="col-sm-3"><label for="tw">Town</label></div>
<div class="col-sm-9">
    <select id="town" name="town">
        <option value="">Select Town</option>

    </select>
</div>


<script type="text/javascript">
$(document).ready(function() {
    $("#region").change(function() {
        $("#town").load("getter.php?choice=" + $("#region").val());
    });
});
</script>

这是getter.php

Here is getter.php

<?php
include "../areashoppers/includes/cs_functions.php";
$sql = "SELECT distinct town FROM oic_areas_full WHERE region = '".$choice."' ORDER BY town ASC";
$st = $sc_conn->query($sql);
while ($r = $st->fetch()) {
    echo "<option>" . $r['town'] . "</option>";
}
?>

这是错误日志中显示的内容

This is what shows in the error log

[2016年8月2日18:39:50 UTC] PHP注意:未定义的变量:在第3行的/home/areashoppers/public_html/nylb/getter.php中进行选择

[02-Aug-2016 18:39:50 UTC] PHP Notice: Undefined variable: choice in /home/areashoppers/public_html/nylb/getter.php on line 3

[2016年8月2日18:39:50 UTC] PHP致命错误:在第5行的/home/areashoppers/public_html/nylb/getter.php中的布尔值上调用成员函数fetch()

[02-Aug-2016 18:39:50 UTC] PHP Fatal error: Call to a member function fetch() on boolean in /home/areashoppers/public_html/nylb/getter.php on line 5

$ sc_conn是我在cs_functions中定义的DSN

$sc_conn is my DSN defined in cs_functions

推荐答案

您必须通过GET方法从网址中进行选择.

you have to get the choice from the url by GET method.

<?php
include "../areashoppers/includes/cs_functions.php";
$choice = $_GET['choice'];
$sql = "SELECT distinct town FROM oic_areas_full WHERE region='$choice' ORDER BY town ASC";
$st = $sc_conn->query($sql);
while ($r = $st->fetch()) {
  echo "<option>" . $r['town'] . "</option>";
}
?>

这篇关于根据第一选择列表值填充第二选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆