首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用mysql DB、PHP和jQuery动态地进行可靠的选择

如何使用mysql DB、PHP和jQuery动态地进行可靠的选择
EN

Stack Overflow用户
提问于 2014-02-17 22:39:37
回答 1查看 220关注 0票数 0

我有三个代表州、城市和银行的选择。

您可以在这个小提琴中看到一个概念的证明

因此,当用户选择一个状态时,链接会发生变化(通过jQuery),并进入所有状态的列表。

如果用户选择一个州和一个城市,则链接再次更改,并转到该状态中所有城市的列表中。

如果用户也选择了银行,则链接将更改到该州、城市和银行的所有办事处的列表。

这些数据来自两个模式,简化如下:

代码语言:javascript
复制
mysql> describe office;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| gid   | int(11)  | YES  |     | NULL    |       |
| name  | longtext | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+

mysql> describe geo;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| gid   | int(11)      | NO   |     | 0       |       |
| name  | varchar(255) | NO   |     |         |       |
| slug  | varchar(255) | NO   |     |         |       |
+-------+--------------+------+-----+---------+-------+

我可以使用php执行如下操作的状态的数据加载第一个select:

代码语言:javascript
复制
<?php
$result = db_query('select * from ptm_geo where level = 1;');
while ($node = db_fetch_object($result)) {
    $states .= "<option value='$node->slug'>$node->name</option>\n";
}
?>
<select id="state">
  <option value=''>Estado</option>
  <?php echo $states ?>
</select>

但我不知道如何使城市和银行从数据库中加载,并成为依赖者。

也就是说,如果您选择一个州,则应该从数据库中加载来自该州的城市。

或者,如果选择了一个州和一个城市,那么只能装载来自该州和城市的银行。

在如何处理这个问题上,任何一个人都能指出正确的方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-17 23:46:27

代码语言:javascript
复制
<script>
    function updateCitiesSelectOptions(state) {
    // Perform a request to your php an return in your case the html
    // is also common to return just an array with the elements 
    // and print the html with javascript

        // https://api.jquery.com/jQuery.get/
        $.get( '/path/to/script/returning/the/html', { state: state }
        .done(function( data ) {
            // here the cities selector is updated
            $("#cities").html(data);
        });
    }

    function updateBanksSelectOptions(city) {
        $.get( '/path/to/script/returning/the/html', { city: city }
        .done(function( data ) {
            // here the banks selector is updated
            $("#banks").html(data);
        });
    }

    // Bind the functions to the change events of the selectors.
    $("#state").bind("change",function(){
        updateCitiesSelectOptions($(this).val());
    });

    $("#cities").bind("change",function(){
        // If you need the state for the query send it here also
        updateBanksSelectOptions($(this).val());
    });
</script>

<?php
    // You will receive the state and the city in $_GET variable as we are performing a
    // $.get javascript call

    // Escape the input preventing MySQL injections
    // Note that the method is deprecated and consider to update your database access
    // http://php.net/manual/es/function.mysql-real-escape-string.php  
    $state = mysql_real_escape_string($_GET["state"]);
    $result = db_query("select * from table where state = $state");
    while ($node = db_fetch_object($result)) {
        echo "<option value='$node->slug'>$node->name</option>\n";
    }
?>

// You will need different scripts to return the html or only one being parameterized. 

// i.e. 
// Data sent via javascript
{ action: "cities", state: state }

// Data sent via javascript
{ action: "banks", cities: state }

<?php 
    if ($_GET['action'] == "cities") 
        // Return cities <options> 
    if ($_GET['action'] == "banks") 
        // Return bank <options> 
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21840718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档