Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 两个动态下拉列表_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 两个动态下拉列表

Javascript 两个动态下拉列表,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有两个下拉列表。当我选择第一个时。第二个应该根据第一个选择的值从数据库中填充。我不想先提交然后再获取值,所以基本上没有页面刷新。我需要jquery和Ajax吗?你不需要jquery,但它让Ajax更容易 另外,您不需要ajax,但是如果不使用它,您必须在初始页面加载的第二个组合中加载整个数据库。您不需要jquery,但是它使ajax变得更容易 另外,您不需要ajax,但如果不使用它,则必须在初始页面加载时为第二个组合加载整个数据库。如果页面上已经有数据,则不需要jQuery,也不需要ajax

我有两个下拉列表。当我选择第一个时。第二个应该根据第一个选择的值从数据库中填充。我不想先提交然后再获取值,所以基本上没有页面刷新。我需要jquery和Ajax吗?

你不需要jquery,但它让Ajax更容易


另外,您不需要ajax,但是如果不使用它,您必须在初始页面加载的第二个组合中加载整个数据库。

您不需要jquery,但是它使ajax变得更容易


另外,您不需要ajax,但如果不使用它,则必须在初始页面加载时为第二个组合加载整个数据库。

如果页面上已经有数据,则不需要jQuery,也不需要ajax


但是,如果您有大量数据,建议使用AJAX。

您不需要jQuery,如果页面上已经有数据,也不需要AJAX


但是,如果您有大量数据,建议您使用AJAX。

虽然jQuery和AJAX不是严格要求的,但如果您希望以未来开发人员知道发生了什么的方式快速实现这一点,则建议您使用AJAX。
第一个下拉菜单应该在更改后启动,通过AJAX在JSON列表中拉取第二个下拉菜单的内容,并自动填充第二个下拉菜单。

虽然jQuery和AJAX不是严格要求的,但如果您希望以未来开发人员知道发生了什么的方式快速完成这项工作,它们是必需的。
第一个下拉列表应在更改后触发,以通过JSON列表中的AJAX拉取第二个下拉列表的内容,并自动填充第二个下拉列表。

Aditya…看看这个AJAX三重下拉列表是否会对您有所帮助,因为它是一个 我的学习教程(杰森·卡夫-info@inks-etc.org)。我仍在和你一起学习 AJAX,并开始学习JQUERY,所以我的球赛还没有结束

ajax\u statecity\u index.php

<html>
<head>
<title>Inks Etc. Triple Ajax Dropdown List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
// Inks Etc. Ajax dropdown code with php
// Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //function to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}

function getState(country_ID) {
    <!--?country= comes from the table "country" not the column "country_name"--
>
    var strURL="../AJAX/ajax_findstate.php?country="+country_ID;  //+country_ID
is a column from the Table: states
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        

document.getElementById('statediv').innerHTML=req.responseText;
                    //alert("Selected Country")
                } else {
                    alert("There was a problem while using  
XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }       
}
function getCity(country_ID,state_ID) {
    //?country= comes from the table name of "country" not the column name 
"country_name"
    var strURL="../AJAX/ajax_findcity.php?
country="+country_ID+"&states="+state_ID;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {


document.getElementById('citydiv').innerHTML=req.responseText;
                    //alert("Selected State");
                } else {
                    alert("There was a problem while using
XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td width="150">
    <select name="country" onChange="getState(this.value)">
        <option value="">Select Country</option>
        <option value="1">USA</option>
        <option value="2">Canada</option>
        <!--<option value="3">Brother</option>
        <option value="4">Canon</option>
        <option value="5">Dell</option>
        <option value="6">Epson</option>
        <option value="7">HP</option>
        <option value="8">Lexmark</option> -->
    </select>
 </td>
 </tr>
 <tr style="">
    <td>State</td>
    <td>
    <div id="statediv">
        <select name="states">
            <option>Select State</option>
        </select>
    </div>
   </td>
  </tr>
  <tr style="">
   <td>City</td>
   <td>
    <div id="citydiv">
        <select name="cities">
            <option>Select City</option>
        </select>
    </div>
  </td>
 </tr>
</table>
</form>
</body>
</html>

**ajax_findstate.php**

<?php
//the $_GET name of country is the dbtable name
$country=intval($_GET['country']);
include "../SearchEngine/dbc_work2014.php";<!--this is where I keep my phpmyadmin entry 
of dbname, dbpass, dbhost, dblocal and save it in a separate file so include in any
file -->

$query="SELECT `id`,`state_name`, `country_ID` FROM states WHERE country_ID='$country'";
$result=mysqli_query($dbc,$query) or die(mysqli_error($dbc));

?>
<select name="states" onchange="getCity(<?=$country?>,this.value)">
<option>Select State</option>
<?
while($row=mysqli_fetch_array($result))
    {
?>
        <option value=<?=$row['id']?>>
            <?=$row['state_name']?>
        </option>
<?
    }
?>
</select>

**ajax_findcity.php**

<?php
session_start();
//echo "{$_SESSION['username']}";
?>
 <?php
 $countryId=intval($_GET['country']);
 $stateId=intval($_GET['states']);
 include "../SearchEngine/dbc_work2014.php";

 //$_SESSION['username'] = $username;// which is $_POST['username']
 //$_SESSION['password'] = $password;// which is $_POST['password']

$query="SELECT `id`,`city_name` FROM cities WHERE country_ID='$countryId' AND
state_ID='$stateId'";
$result=mysqli_query($dbc, $query)or die(mysqli_error($dbc));

//$sql = "UPDATE `SignUp` SET `Tampa` WHERE `id`='1'";
//echo $sql;
?>

<select name="cities">
<option>Select City</option>

<?php
    while($row=mysqli_fetch_array($result))
    {
?>
<option value><?=$row['city_name']?></option>

<? }

?>
</select>

Place 3 Tables in a phpmyadmin database:

Table:  Country            
id_country    country_name  
1             USA
2             Canada
3             India           

Table: State
id_state   state_name id_country 
1          Florida
2          Quebec
3          Maharashtra


Table:  City
id_city   city_name   id_state   id_country
1         Sarasota    1          1
2         Montreal    2          2
3         Mumbai      3          3

Any questions email Jason Kraft a text...but I am far from being near perfect in fact I
feel quite elementary with all that I need to know.  

Good Luck...Aditya

墨水等三重Ajax下拉列表
//墨水等。用php编写的Ajax下拉代码
//罗山·巴塔拉伊-nepaliboy007@yahoo.com
//如果您有任何问题,请联系我http://roshanbh.com.np
函数getXMLHTTP(){//返回xml http对象的函数
var xmlhttp=false;
试一试{
xmlhttp=新的XMLHttpRequest();
}
第(e)款{
试试{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
捕获(e){
试一试{
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
渔获物(e1){
xmlhttp=false;
}
}
}
返回xmlhttp;
}
函数getState(国家/地区ID){
陈述
选择状态
城市
选择城市
**ajax_findstate.php**
>
**ajax_findcity.php**

Aditya…看看这个AJAX三重下拉列表是否会对您有所帮助,因为它是一个 我的学习教程(杰森·卡夫-info@inks-我仍在学习 AJAX,并开始学习JQUERY,所以我的球赛还没有结束

ajax\u statecity\u index.php

<html>
<head>
<title>Inks Etc. Triple Ajax Dropdown List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" type="text/javascript">
// Inks Etc. Ajax dropdown code with php
// Roshan Bhattarai - nepaliboy007@yahoo.com
// If you have any problem contact me at http://roshanbh.com.np
function getXMLHTTP() { //function to return the xml http object
    var xmlhttp=false;  
    try{
        xmlhttp=new XMLHttpRequest();
    }
    catch(e)    {       
        try{            
            xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e){
            try{
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e1){
                xmlhttp=false;
            }
        }
    }

    return xmlhttp;
}

function getState(country_ID) {
    <!--?country= comes from the table "country" not the column "country_name"--
>
    var strURL="../AJAX/ajax_findstate.php?country="+country_ID;  //+country_ID
is a column from the Table: states
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        

document.getElementById('statediv').innerHTML=req.responseText;
                    //alert("Selected Country")
                } else {
                    alert("There was a problem while using  
XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }       
}
function getCity(country_ID,state_ID) {
    //?country= comes from the table name of "country" not the column name 
"country_name"
    var strURL="../AJAX/ajax_findcity.php?
country="+country_ID+"&states="+state_ID;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {


document.getElementById('citydiv').innerHTML=req.responseText;
                    //alert("Selected State");
                } else {
                    alert("There was a problem while using
XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}
</script>
</head>
<body>
<form method="post" action="" name="form1">
<table width="300" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td width="150">
    <select name="country" onChange="getState(this.value)">
        <option value="">Select Country</option>
        <option value="1">USA</option>
        <option value="2">Canada</option>
        <!--<option value="3">Brother</option>
        <option value="4">Canon</option>
        <option value="5">Dell</option>
        <option value="6">Epson</option>
        <option value="7">HP</option>
        <option value="8">Lexmark</option> -->
    </select>
 </td>
 </tr>
 <tr style="">
    <td>State</td>
    <td>
    <div id="statediv">
        <select name="states">
            <option>Select State</option>
        </select>
    </div>
   </td>
  </tr>
  <tr style="">
   <td>City</td>
   <td>
    <div id="citydiv">
        <select name="cities">
            <option>Select City</option>
        </select>
    </div>
  </td>
 </tr>
</table>
</form>
</body>
</html>

**ajax_findstate.php**

<?php
//the $_GET name of country is the dbtable name
$country=intval($_GET['country']);
include "../SearchEngine/dbc_work2014.php";<!--this is where I keep my phpmyadmin entry 
of dbname, dbpass, dbhost, dblocal and save it in a separate file so include in any
file -->

$query="SELECT `id`,`state_name`, `country_ID` FROM states WHERE country_ID='$country'";
$result=mysqli_query($dbc,$query) or die(mysqli_error($dbc));

?>
<select name="states" onchange="getCity(<?=$country?>,this.value)">
<option>Select State</option>
<?
while($row=mysqli_fetch_array($result))
    {
?>
        <option value=<?=$row['id']?>>
            <?=$row['state_name']?>
        </option>
<?
    }
?>
</select>

**ajax_findcity.php**

<?php
session_start();
//echo "{$_SESSION['username']}";
?>
 <?php
 $countryId=intval($_GET['country']);
 $stateId=intval($_GET['states']);
 include "../SearchEngine/dbc_work2014.php";

 //$_SESSION['username'] = $username;// which is $_POST['username']
 //$_SESSION['password'] = $password;// which is $_POST['password']

$query="SELECT `id`,`city_name` FROM cities WHERE country_ID='$countryId' AND
state_ID='$stateId'";
$result=mysqli_query($dbc, $query)or die(mysqli_error($dbc));

//$sql = "UPDATE `SignUp` SET `Tampa` WHERE `id`='1'";
//echo $sql;
?>

<select name="cities">
<option>Select City</option>

<?php
    while($row=mysqli_fetch_array($result))
    {
?>
<option value><?=$row['city_name']?></option>

<? }

?>
</select>

Place 3 Tables in a phpmyadmin database:

Table:  Country            
id_country    country_name  
1             USA
2             Canada
3             India           

Table: State
id_state   state_name id_country 
1          Florida
2          Quebec
3          Maharashtra


Table:  City
id_city   city_name   id_state   id_country
1         Sarasota    1          1
2         Montreal    2          2
3         Mumbai      3          3

Any questions email Jason Kraft a text...but I am far from being near perfect in fact I
feel quite elementary with all that I need to know.  

Good Luck...Aditya

墨水等三重Ajax下拉列表
//墨水等。用php编写的Ajax下拉代码
//罗山·巴塔拉伊-nepaliboy007@yahoo.com
//如果您有任何问题,请联系我http://roshanbh.com.np
函数getXMLHTTP(){//返回xml http对象的函数
var xmlhttp=false;
试一试{
xmlhttp=新的XMLHttpRequest();
}
第(e)款{
试试{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
捕获(e){
试一试{
xmlhttp=新的ActiveXObject(“Msxml2.xmlhttp”);
}
渔获物(e1){
xmlhttp=false;
}
}
}
返回xmlhttp;
}
函数getState(国家/地区ID){
陈述
选择状态
城市
选择城市
**ajax_findstate.php**
>
**ajax_findcity.php**

如何在不加载整个数据库的情况下继续进行?@Aditya-询问如何进行是一个大问题。您可能想了解jQuery的ajax功能:以及如何响应http请求,从您选择的服务器端语言进行数据库查找,试一试,如果仍然有问题,然后返回一个新问题。同时,您应该接受这个答案;)@aditya shukla,你应该自己解决这个问题,遇到问题时再问一个新问题。这个网站上有很多关于层叠下拉列表的问题。@aditya,你可以检查一下:。这里有一个stackoverflow问题:如何在不加载整个数据库的情况下继续?@aditya-问你怎么样一个大问题。你想看看jQuery的ajax功能:以及如何响应http请求,从你选择的服务器端语言进行数据库查找,试一试,如果你仍然有问题,然后带着一个新问题回来。同时,你应该接受这个答案;)@aditya shukla,你应该自己解决这个问题,并提问遇到问题时会出现一个新问题。这个网站上有很多关于层叠下拉列表的问题。@Aditya,你可以查看:。这里有一个关于堆栈溢出的问题:感谢指导。我必须学习ajax才能利用它。感谢指导。我必须学习ajax才能利用它。