简体   繁体   English

动态填充下拉框不适用于“空格”

[英]dynamically populating drop down box not working with 'spaces'

I have a dynamic drop down box which one the first selection, populates the second drop down from the database. 我有一个动态下拉框,其中第一个选择是从数据库中填充第二个下拉列表。

include("connect.php");
$choice = mysql_real_escape_string($_GET['choice']);

$query = "SELECT * FROM locations WHERE county_name='$choice' ORDER BY town_name ASC";

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
        echo "<option>" . $row{'town_name'} . "</option>";
}

This works perfectly for any 'county_name' variable that is 1 word ie London, Essex. 完美适用于任何1个单词的“ county_name”变量,即伦敦,艾塞克斯。 But when they pick 'East Sussex' for example, it returns nothing. 但是,例如当他们选择“东萨塞克斯郡”时,它什么也不会返回。

I thought the mysql_real_escape_string was the problem, so I removed that and yet the problem was still occurring. 我以为mysql_real_escape_string是问题,所以我删除了它,但问题仍然存在。

The javascript to plug the data into the second drop down is as follows: 将数据插入第二个下拉列表的JavaScript如下:

$(function() {              
$("#select_county").change(function() {
$("#select_town").load("include_mx/getter.php?choice=" + $("#select_county").val());
    });     
});

Any help would be appreciated as to why the 'spaces' in-between are having no success. 对于为什么它们之间的“空间”没有成功的任何帮助,将不胜感激。

Many thanks in advance. 提前谢谢了。

I am unsure of the quickest most simplest answer, how ever, this works for me: 我不确定最快,最简单的答案,但这对我来说有多有效:

Any county with a 'space' has been replaced with 'West+Sussex': 任何具有“空间”的县均已被“西部+苏塞克斯”所取代:

`str_replace(" ","+","$variable");

Because I didn't want to show the '+'s I used this: 因为我不想显示'+',所以我使用了这个:

$c=$row['county_name']; $county = str_replace("+"," ","$c"); echo "<option value='$c'>$county</option>";

That seem's to work all the time now and is pretty robust. 这似乎一直在起作用,并且非常可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM