请参阅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);
?>
最佳答案
最好的做法是使用 xmlhttp 对象来加载回显数据的 PHP 页面。从该 xmlhttp 对象中,您可以将 responseText(这将是 PHP 页面的输出内容)分配给 Javascript 变量。
换句话说,您可能想使用 AJAX .
关于php - 根据下拉列表使用数据库值动态更新 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11017549/