用php和ajax下拉菜单 [英] dropdown with php and ajax

查看:46
本文介绍了用php和ajax下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在努力解决这个问题,但是我并没有得出任何结论,因此想在这里寻求帮助.问题是我得到的是空白下拉列表,而不是应该从数据库中获取填充的城市列表.数据库连接很好,但下拉菜单中没有任何内容.

For some time I am battling to solve this problem but I am not coming to any conclusion so thought to seek some help here. The problem is that I am getting a blank dropdown instead I should get list of cities populated from the database. Database connection is fine but I am not getting anything in my dropdown.

这就是我正在做的:

    <?php

require 'includes/connect.php'; - database connection
$country=$_REQUEST['country']; - get from form (index.php)
$q = "SELECT city FROM city where countryid=".$country;    
$result = $mysqli->query($q) or die(mysqli_error($mysqli));    
if ($result) {
?>  
    <select name="city">
    <option>Select City</option>
    $id = 0;
    <?php while ($row = $result->fetch_object()) { 
       $src = $row->city;
       $id = $id + 1;      
    ?>      
    <option value= <?php $id ?> > <?php $src ?></option>
<?php } ?>
</select>
<?php } ?>

ajax脚本是这样的:

ajax script is this:

    <script>
    function getXMLHTTP() { //function to return the xml http object
            var xmlhttp=false;  
            try{mlhttp=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) {                      
                        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>

这是我的表单代码:

<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">New Zealand</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>

</table>
</form>

推荐答案

我认为问题在于您正在输出< option> 标记.

I think the problem is where you are outputting the <option> tags.

尝试在您的< select> 标签之间使用此代码块.

Try using this block of code between your <select> tags.

    <option>Select City</option>
    <?php 
       $id = 0;
       while ($row = $result->fetch_object()) { 
       $src = $row->city;
       $id = $id + 1;      
    ?>          
    <option value="<?php echo htmlspecialchars($id,ENT_QUOTES) ?>"><?php echo htmlspecialchars($src) ?></option>
<?php } ?>

为明确起见,在 $ id $ src 变量之前没有任何 echo 语句.我添加了 htmlspecialchars()作为生成正确转义的html的习惯.

To clarify, you didn't have any echo statements before the $id and $src variables. I added htmlspecialchars() as a habit to produce properly escaped html.

这篇关于用php和ajax下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆