用php和ajax下拉菜单

编程入门 行业动态 更新时间:2024-10-11 11:13:07
本文介绍了用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下拉菜单

本文发布于:2023-10-10 17:45:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:菜单   php   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!