本文介绍了在使用PHP-AJAX另一个下拉的变化更新下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变对国家下拉改变城市下拉列表中值。现在好了,停留在点上,当我选择它说,他变量$ r是不确定该国的价值。页:的index.php

 <形式方法=邮报行动=NAME =Form1的>
<表格的宽度=60%的边界=0CELLSPACING =0的cellpadding =0>
  &其中; TR>
    < TD WIDTH =150>国家< / TD>
    < TD WIDTH =150><选择名称=国家的onChange =getCity('findcity.php国家=?'+ THIS.VALUE)>
    <期权价值=>选择国家< /选项>
    <期权价值=1>美国< /选项>
    <期权价值=2>加拿大和LT; /选项>
        < /选择>< / TD>
  < / TR>
  < TR风格=>
    < TD>城市和LT; / TD>
    < TD>< D​​IV ID =citydiv><选择名称=城与GT;
    <选项>选择城市< /选项>
        < /选择>< / DIV>< / TD>
  < / TR>
  &其中; TR>
    < TD>&安培; NBSP;< / TD>
    < TD>&安培; NBSP;< / TD>
  < / TR>
  &其中; TR>
    < TD>&安培; NBSP;< / TD>
    < TD>&安培; NBSP;< / TD>
  < / TR>
< /表>
 

页:findcity.php

 < PHP
$国家= $ _ REQUEST ['国家'];
$链接=的mysql_connect('本地主机','根','');
如果(!$链接){
    死亡(无法连接:mysql_error());
}
mysql_select_db('db_ajax');
$查询=从城市里countryid = $国家选择的城市;
$结果= mysql_query($查询);
?>
<选择名称=城市与GT;

&LT ;?而($ R = mysql_fetch_array($结果)){

?>
<期权价值=>< PHP的echo $ R ['城市'];?>< /选项>
&LT ;? ?}>
< /选择>
 

这里是脚本

 <脚本>
功能getXMLHTTP(){//函数返回的XML HTTP对象
        VAR XMLHTTP =假;
        尝试{
            XMLHTTP =新XMLHtt prequest();
        }
        赶上(E){
            尝试{
                XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
            }
            赶上(E){
                尝试{
                XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);
                }
                赶上(E1){
                    XMLHTTP =假;
                }
            }
        }

        返回XMLHTTP;
    }



    功能getCity(strURL){

        VAR REQ = getXMLHTTP();

        如果(REQ){

            req.onreadystatechange =功能(){
                如果(req.readyState == 4){
                    //只有当OK
                    如果(req.status == 200){
                        的document.getElementById('citydiv)的innerHTML = req.responseText。
                    } 其他 {
                        警报(有同时使用XMLHTTP是个问题:\ N+ req.statusText);
                    }
                }
            }
            req.open(GET,strURL,真正的);
            req.send(空);
        }

    }
< / SCRIPT>
 

解决方案

请尝试使用 mysql_fetch_assoc 而不是 mysql_fetch_array

I want to change the value of city drop-down on change of country drop-down. Well now stuck at on point when I select the value in the country it says he variable $r is undefined.page : index.php

<form method="post" action="" name="form1">
<table width="60%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="150">Country</td>
    <td  width="150"><select name="country" onChange="getCity('findcity.php?country='+this.value)">
    <option value="">Select Country</option>
    <option value="1">USA</option>
    <option value="2">Canada</option>
        </select></td>
  </tr>
  <tr style="">
    <td>City</td>
    <td ><div id="citydiv"><select name="city">
    <option>Select City</option>
        </select></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

page: findcity.php

<?php 
$country=$_REQUEST['country'];
$link = mysql_connect('localhost', 'root', ''); 
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="select city from city where countryid=$country";
$result=mysql_query($query);
?>
<select name="city">

<? while($r=mysql_fetch_array($result)) { 

?>
<option value=""><?php echo $r['city'];?></option>
<? } ?>
</select>

here is the script

<script>
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 getCity(strURL) {      

        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;                      
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }               
            }           
            req.open("GET", strURL, true);
            req.send(null);
        }

    }
</script>
解决方案

try using mysql_fetch_assoc instead of mysql_fetch_array

这篇关于在使用PHP-AJAX另一个下拉的变化更新下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:55