我使用下拉列表,并希望根据列表中选定的项在同一页的表上显示内容。
此表的数据使用SQL从数据库中检索。我只想显示数据库中的“运河”列与下拉列表中选定的项相等的数据。
这是我在PHP中的代码:
<head>
<meta charset="utf-8">
</head>
<form name='choose-channel'>
<select name='channel'>
<option selected='true' disabled='true'> Choose one channel </option>
<option value='Option-1'> Option-1 </option>
<option value='Option-2'> Option-2 </option>
<option value='Option-2'> Option-3 </option>
</select>
</form>
<?php
//Conect with the database
require_once 'conectar-database.php';
$query = "SELECT id_sku FROM database.table";
$result = mysql_query($query);
echo "<table border='1' >";
echo '<tr>';
echo '<th> id_sku </th>';
echo '</tr>';
while($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td>'.$row['id_sku'].'</td>';
echo '</tr>';
}
echo '</table>';
?>
<body>
<body>
</html>我想在查询中添加这样的内容:'WHERE channel = $ channel ',这个通道变量将从下拉列表中选择值。我想我需要用javascrip做点什么,但我不知道怎么做。
谢谢
发布于 2014-06-30 08:11:54
正如您所说,要在不刷新页面的情况下显示数据,则需要使用阿贾克斯。您可以在朗读上了解更多关于阿贾克斯的信息,这样才能使其正常工作。
我希望这能帮到你。
PHP视图页:
<script type="text/javascript">
$(document).ready(function() {
$("#channel").change(function(){
$.post( "ajax.php", { channel: $(this).val() })
.success(function(data) {
$(".result").html(data);
});
});
});
</script>
<select name='channel'>
<option selected='true' disabled='true'> Choose one channel </option>
<option value='Option-1'> Option-1 </option>
<option value='Option-2'> Option-2 </option>
<option value='Option-2'> Option-3 </option>
</select>
<div class="result"></div>ajax.php (保存在该位置):
<?php
//get value from page
$channel = $_POST['channel'];
//Conect with the database
require_once 'conectar-database.php';
$query = "SELECT id_sku FROM database.table where channel =$channel";
$result = mysql_query($query);
$msg ='';
$msg = $msg." <table border='1' >";
$msg = $msg. '<tr>';
$msg = $msg. '<th> id_sku </th>';
$msg = $msg. '</tr>';
while($row = mysql_fetch_array($result)) {
$msg = $msg. '<tr>';
$msg = $msg. '<td>'.$row['id_sku'].'</td>';
$msg = $msg. '</tr>';
}
$msg = $msg. '</table>';
echo $msg;
?>https://stackoverflow.com/questions/24481997
复制相似问题