在列表框中动态注入一些内容,并使用PHP将其取回
我创建了一个网页,它使用JQuery将表单的内容重定向到另一个网页,使用PHP连接到数据库以查找一些内容并将其放回第一页 一切都很好(多亏了stack overflow的追随者的帮助:-),但现在我想知道:我要的是一个城市的邮政编码,如果我幸运的话,这个邮政编码是唯一的(只有一个城市有)但也有一些城市的邮政编码是相同的,所以我想在这种情况下显示一个列表框,供用户选择他/她的城市。 有人知道怎么做吗 我的代码: home.html在列表框中动态注入一些内容,并使用PHP将其取回,php,jquery,forms,webpage,Php,Jquery,Forms,Webpage,我创建了一个网页,它使用JQuery将表单的内容重定向到另一个网页,使用PHP连接到数据库以查找一些内容并将其放回第一页 一切都很好(多亏了stack overflow的追随者的帮助:-),但现在我想知道:我要的是一个城市的邮政编码,如果我幸运的话,这个邮政编码是唯一的(只有一个城市有)但也有一些城市的邮政编码是相同的,所以我想在这种情况下显示一个列表框,供用户选择他/她的城市。 有人知道怎么做吗 我的代码: home.html <!DOCTYPE html> <html>
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<form action="/" id="myform">
<input type="text" name="postal_code" id="postal_code" placeholder="Search..." />
<input type="submit" value="Search" />
</form>
<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>
<script>
$('#myform').submit(function() {
var url = 'target.php';
var postal_code = $('#postal_code').val();
$.post( url, { postal_code: postal_code },
function( data ) {
$( "#result" ).empty().append( data );
}
);
return false;
});
$('#myform')。提交(函数(){
var url='target.php';
var postal_code=$(“#postal_code”).val();
$.post(url,{邮政编码:邮政编码},
函数(数据){
$(“#结果”).empty().append(数据);
}
);
返回false;
});
target.php
<?php
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$bdd = new PDO('mysql:host=localhost;dbname=mydatabase', 'root', '', $pdo_options);
$response = $bdd->prepare('SELECT city FROM city_list where postal_code = ?');
$response->execute(array($_POST['postal_code']));
echo '<ul>';
while ($data = $response->fetch())
{
?>
<br/>The city you entered the postal code is : <?php echo $data['city'];
}
$response->closeCursor();
}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
?>
您输入的邮政编码是:
编辑:
这是适合我需要的代码。我只想对朱尔斯的代码做一些很小的修改,使其正常(原因不得而知,他的答案对他来说非常有效,但对我来说却不行:-)
我不确定您正在使用哪个库进行数据库查询,所以我将使用伪代码和mysql\u查询 target.php
<?php
try {
//Get the postal code:
$postcode = $_POST['postal_code'];
//Make MySQL connection
mysql_connect("localhost", "username", "password") or die (mysql_error());
//Select the database
mysql_select_db("mydatabase");
//Do your query based on the postcode...
$query = "SELECT city FROM city_list where postal_code = '" . mysql_real_escape_string($postcode) . "'";
//Return the response in a variable
$data = mysql_query($query);
//Check how many rows the query returned. If more than 1 that means several cities
//exist for one postcode, so you should show a listbox.
//If not, just return the city name
if (mysql_num_rows($data) > 1) { ?>
<select name="cities" multiple="multiple">
<? while ($row = mysql_fetch_assoc($data)) { ?>
<option value="<?=$row['city']?>"><?=$row['city']?></option>
<? } ?>
</select>
<? }
else {
$row = mysql_fetch_assoc($data);
echo $row['city'];
}
}
catch (Exception $e) {
die("Error : " . $e->getMessage());
}
?>
我希望你明白我的意思,你可以自己完成。如果有很多城市,
$data['city']
是数组还是长字符串?返回列表是个好主意,但是在target.php中,你打开了
标记,但你没有用填充它,也没有用
标记关闭它。因此,我将返回一个- 纽约市
- 华盛顿特区
列表,并将其放入结果区。因此,您做得很好。我看不出你需要什么帮助:)@k102在这种情况下是字符串。@Jules谢谢Jules:-)。问题是,当有多个城市的邮政编码相同时,我想显示一个列表框,供用户选择他/她的城市。@Bruno,你已经修复了它。因此,问题是我使用了缩短的PHP开头标记,感谢您的代码。我粘贴了它,但我不明白为什么它不工作(什么都没有发生)。嗯,因为代码不完整。如果你愿意,我可以编辑这篇文章来完成它。我非常感激,因为我试图用我的数据来调整它,但它不起作用。我一直在搜索,但我没有找到问题的根源。我更新了我的答案。看一看,稍作修改以符合您的需要,并让我知道它是否解决了您的问题。:)谢谢你的代码,但它仍然不起作用(什么都没有发生),我不知道为什么。
<?php
try {
//Get the postal code:
$postcode = $_POST['postal_code'];
//Make MySQL connection
mysql_connect("localhost", "username", "password") or die (mysql_error());
//Select the database
mysql_select_db("mydatabase");
//Do your query based on the postcode...
$query = "SELECT city FROM city_list where postal_code = '" . mysql_real_escape_string($postcode) . "'";
//Return the response in a variable
$data = mysql_query($query);
//Check how many rows the query returned. If more than 1 that means several cities
//exist for one postcode, so you should show a listbox.
//If not, just return the city name
if (mysql_num_rows($data) > 1) { ?>
<select name="cities" multiple="multiple">
<? while ($row = mysql_fetch_assoc($data)) { ?>
<option value="<?=$row['city']?>"><?=$row['city']?></option>
<? } ?>
</select>
<? }
else {
$row = mysql_fetch_assoc($data);
echo $row['city'];
}
}
catch (Exception $e) {
die("Error : " . $e->getMessage());
}
?>