首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用multiple selection下拉菜单从countries表中选择country时,如何从states表中获取countries states列表?

当我使用multiple selection下拉菜单从countries表中选择country时,如何从states表中获取countries states列表?
EN

Stack Overflow用户
提问于 2013-03-12 16:21:46
回答 3查看 3.4K关注 0票数 0

当我使用多选下拉列表从countries表中选择country时,如何从states表中获取countries states?这是我的代码。

mysql表

代码语言:javascript
复制
CREATE TABLE `countries` (
  `countryID` varchar(3) NOT NULL default '',
  `countryName` varchar(52) NOT NULL default '',
  `localName` varchar(45) NOT NULL,
  `webCode` varchar(2) NOT NULL,
  `region` varchar(26) NOT NULL,
  `continent` enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL,
  `latitude` double NOT NULL default '0',
  `longitude` double NOT NULL default '0',
  `surfaceArea` float(10,2) NOT NULL default '0.00',
  `population` int(11) NOT NULL default '0',
  PRIMARY KEY  (`countryID`),
  UNIQUE KEY `webCode` (`webCode`),
  UNIQUE KEY `countryName` (`countryName`),
  KEY `region` (`region`),
  KEY `continent` (`continent`),
  KEY `surfaceArea` (`surfaceArea`),
  KEY `population` (`population`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

states表的结构:

代码语言:javascript
复制
CREATE TABLE `states` (
  `stateID` smallint(5) unsigned NOT NULL auto_increment,
  `stateName` varchar(50) NOT NULL default '',
  `countryID` varchar(3) NOT NULL,
  `latitude` double NOT NULL default '0',
  `longitude` double NOT NULL default '0',
  PRIMARY KEY  (`stateID`),
  KEY `stateName` (`stateName`),
  KEY `countryID` (`countryID`),
  KEY `unq` (`countryID`,`stateName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

countries表的数据(限制为0,500)

代码语言:javascript
复制
INSERT INTO `countries` (`countryID`, `countryName`, `localName`, `webCode`, `region`, `continent`, `latitude`, `longitude`, `surfaceArea`, `population`) VALUES 
  ('BRA','Brazil','Brasil','BR','South America','South America',-10,-55,8547403.00,170115000),
  ('CHN','China','Zhongquo','CN','Eastern Asia','Asia',35,105,9572900.00,1277558000),
  ('FRA','France','France','FR','Western Europe','Europe',47,2,551500.00,59225700),
  ('IND','India','Bharat/India','IN','Southern and Central Asia','Asia',28.47,77.03,3287263.00,1013662000),
   ('USA','USA','United States','US','North America','North America',38,-97,9363520.00,278357000);
COMMIT;

states表的数据(限制为0,500)

代码语言:javascript
复制
INSERT INTO `states` (`stateID`, `stateName`, `countryID`, `latitude`, `longitude`) VALUES 
  (5,'California','USA',37.42,-122.06),
  (6,'Beijing','CHN',39.93,116.39),
  (9,'Iowa','USA',43.03,-96.09),
  (10,'New York','USA',40.76,-73.97),
  (12,'... ....','CHN',32.06,118.78);
COMMIT;

国家/地区下拉列表

代码语言:javascript
复制
<? $Type_sql="SELECT countryName FROM countries ORDER by countryName ASC";
                                $Type_result=mysql_query($Type_sql);
                                while($Type_rows=mysql_fetch_array($Type_result)){
                                echo "<option value='"; 
                                echo $Type_rows['countryName']; 
                                echo "'>";
                                echo $Type_rows['countryName'];
                                echo "</option>";
                                } ?>
            </select>

状态下拉列表

代码语言:javascript
复制
<? $Type_sql="SELECT stateName FROM countries c, states s where c.countryID = s.countryID ORDER by stateName ASC";
                                $Type_result=mysql_query($Type_sql);
                                while($Type_rows=mysql_fetch_array($Type_result)){
                                echo "<option value='"; 
                                echo $Type_rows['stateName']; 
                                echo "'>";
                                echo $Type_rows['stateName'];
                                echo "</option>";
                                } ?>
            </select>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-13 14:05:29

Hi Ahmed,

请看下面的代码。我重新编写了代码,并修改了多个选项,但我忽略了javascript验证。请处理一下,这个页面的名称是form3.php

代码语言:javascript
复制
<html>
<head>
<script type="text/javascript">
    function viewstates()
    { 
        jQuery('#langucountry').change(function () { });
    }
    $(function() {
        $('#basicmultiselect').multiSelect({select_all_min: 3});
        $('#langucountry').multiSelect({
            select_all_min: 3, no_selection: "Please select!", selected_text: " clicked" });
        $('#methods').multiSelect(); });
</script>
</head>
<body>
<form name="form3" action="form3.php" method="post">
<?php
/****** connection string *******/
/* start */
    $conn = mysql_connect("localhost","root","");
    $dbSelect = mysql_select_db("test");
/* end */

/*----checks whether form is submited -------*/
if(isset($_POST['cbocountry']))
{
    $lstStates = array();
        foreach($_POST['cbocountry'] as $countryCode)
        {
            $fetch_states="SELECT stateName FROM  states where countryID = '$countryCode' ORDER by stateName ASC";
            $Type_result=mysql_query($fetch_states);
            while($Type_rows=mysql_fetch_array($Type_result)){

                        if(!in_array($Type_rows['stateName'],$lstStates))
                            array_push($lstStates,$Type_rows['stateName']);
             } 
        }

}


                                $Type_sql="SELECT countryID,countryName FROM countries ORDER by countryName ASC";
                                $Type_result=mysql_query($Type_sql);
                                echo"Countries:<select  id=\"langucountry\" name=\"cbocountry[]\" class=\"arc90_multiselect\" multiple=\"multiple\" title=\"Languages\"\>";
                                while($Type_rows=mysql_fetch_array($Type_result)){

                                        echo "<option value='".$Type_rows['countryID']."'>";
                                        echo $Type_rows['countryName'];
                                        echo "</option>";
                                } 
                                echo"</select>";





                    echo"States:<select><option value=''>--select--</option>";
                            if(count($lstStates) > 0)
                            {
                                foreach($lstStates as $item)
                                {
                                    echo "<option value='"; 
                                    echo $item; 
                                    echo "'>";
                                    echo $item;
                                    echo "</option>";
                                } 
                            }
                    echo"</select>";

                    echo"<input type=\"submit\" value=\"submit\" text=\"submit\"/>";

?>
</form>
</body>
</html>

享受

票数 0
EN

Stack Overflow用户

发布于 2013-03-12 16:57:00

这可以通过将状态下拉列表的代码放入一个单独的php处理程序中来完成,稍微修改一下:

代码语言:javascript
复制
<select name="state">
<?php 
    $countryID= mysql_real_escape_string($_GET['countryID']);
    $Type_sql="SELECT stateName FROM states s where s.countryID = '$countryID' ORDER by stateName ASC";
                $Type_result=mysql_query($Type_sql);
                while($Type_rows=mysql_fetch_array($Type_result)){
                    echo "<option value='";
                    echo $Type_rows['stateName'];
                    echo "'>";
                    echo $Type_rows['stateName'];
                    echo "</option>";
                } 
?>
</select>

每当国家/地区选择列表中有onChange事件时,javascript将使用Ajax调用此脚本。请参考ajax文档,如http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml或使用jQuery:http://api.jquery.com/jQuery.get/

像你的计划一样修改状态select的内容也是可能的,但是你需要两个选择列表,一个隐藏列表来存储你删除的条目或者全部条目。

票数 0
EN

Stack Overflow用户

发布于 2013-03-12 19:59:23

这是一个完全正常工作的代码。只需将此代码放入php页面,并在mysql_select_db.中将onChange参数中的表单名称更改为您的页面name.Also,然后更改数据库名称

我的页面名称是form1.php

代码语言:javascript
复制
<html>
<body>
<form name="form1">
<?php

if(isset($_REQUEST['stateId']))
{
    $stateId = $_REQUEST['stateId'];
}
else
{
    $stateId = "";
}
$conn = mysql_connect("localhost","root","");
$dbSelect = mysql_select_db("test");
$Type_sql="SELECT countryID,countryName FROM countries ORDER by countryName ASC";
                                $Type_result=mysql_query($Type_sql);
                                echo"Countries:<select onChange=\"window.location='form1.php?stateId='+this.value\">";
                                while($Type_rows=mysql_fetch_array($Type_result)){
                                    if($Type_rows['countryID']==$_REQUEST['stateId'])
                                    {
                                        echo "<option value='".$Type_rows['countryID']."' selected>";
                                        echo $Type_rows['countryName'];
                                        echo "</option>";
                                    }
                                    else
                                    {
                                        echo "<option value='".$Type_rows['countryID']."'>";
                                        echo $Type_rows['countryName'];
                                        echo "</option>";
                                    }
                                } 
                                echo"</select>";

 $Type_sql="SELECT stateName FROM  states where countryID = '$stateId' ORDER by stateName ASC";
                                $Type_result=mysql_query($Type_sql);
                    echo"States:<select><option value=''>--select--</option>";
                                while($Type_rows=mysql_fetch_array($Type_result)){
                                echo "<option value='"; 
                                echo $Type_rows['stateName']; 
                                echo "'>";
                                echo $Type_rows['stateName'];
                                echo "</option>";
                                } 
                    echo"</select>";

?>
</form>
</body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15356291

复制
相关文章

相似问题

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