【发布时间】:2012-06-14 02:51:50
【问题描述】:
请参阅website。
我的数据库有一个表,其中包含值“名称”(左侧第 2 步下的事件)、“价格”、“日期”等,我想在右侧的暗框中动态显示它们,具体取决于选择了哪个事件。
我目前正在使用下面的代码显示事件本身,但我不确定如何开发它以根据此选择获取数据库值。我猜是某种 .get()。
<script type="text/javascript">
$(document).ready(function() {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
$('#club').change(function(event) {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
</script>
这让我很困惑很长时间,所以任何帮助都将非常非常感谢!
编辑
感谢您的回复。这是我现在拥有的,但它不起作用。
jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
$('#club').change(function(event) {
$.ajax({
type: "post",
url: "eventinfo.php",
data: $(this).serialize(),
success: function(data) {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>Price'+data.price);
},
dataType: "json"
});
});
</script>
eventinfo.php:
<?php
// the name of the input text box is 'club'
$night = $_POST['club'];
$night = mysql_real_escape_string($night);
// one of the columns values included in * is 'price'
// this line was previously: $query = mysql_query("SELECT * FROM nights WHERE name = '$night'");
$query = "SELECT * FROM nights WHERE name = '$night'";
$result = mysql_query($query);
$items = array();
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array($row[0]);
}
}
mysql_close();
// convert into JSON format and print
echo json_encode($items);
?>
【问题讨论】:
-
你是对的。使用
$.ajax获取值然后填充。 -
你的数据库中有一行叫做 price?
-
另外,使用 Firebug for Firefox 查看您是否遇到任何错误可能会有所帮助。
-
查看 Firebug 您返回的是一个空数组,这就是您无法获得价格的原因。
-
肯定有一个数据库行叫price。可能是我的 PHP 没有正确创建数组吗?我对 PHP 数组很弱...
标签: php jquery drop-down-menu