我有三个代表州、城市和银行的选择。
您可以在这个小提琴中看到一个概念的证明
因此,当用户选择一个状态时,链接会发生变化(通过jQuery),并进入所有状态的列表。
如果用户选择一个州和一个城市,则链接再次更改,并转到该状态中所有城市的列表中。
如果用户也选择了银行,则链接将更改到该州、城市和银行的所有办事处的列表。
这些数据来自两个模式,简化如下:
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:
<?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>但我不知道如何使城市和银行从数据库中加载,并成为依赖者。
也就是说,如果您选择一个州,则应该从数据库中加载来自该州的城市。
或者,如果选择了一个州和一个城市,那么只能装载来自该州和城市的银行。
在如何处理这个问题上,任何一个人都能指出正确的方向吗?
发布于 2014-02-17 23:46:27
<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>
?>https://stackoverflow.com/questions/21840718
复制相似问题