我有2个选择框选项,item
和part number
。当我选择一个项目时,我正在使用php和ajax来获取部件号。
问题是,html和php都可以工作但是对于ajax。选择part number
时,item
选择框中不会填充任何内容。
这是我的html和ajax端(index.php
):
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function fetch_select(val)
{
$.ajax({
type: 'post',
url: 'get_state.php',
success: function (response) {
document.getElementById("partno").innerHTML=response;
}
});
}
</script>
</head>
<body>
<div id="select_box">
<form action="" method="post">
Item:
<select name="item" id="item" onchange="fetch_select(this.value);">
<option value="">select item</option>
<option value="item1">Item 1</option>
<option value="item2">Item 2</option>
</select>
<br>
Part:
<select name="partno" id="partno">
<option value="">select part</option>
</select>
</form>
</body>
</html>
这是php文件(get_state.php
):
<?php
//Connection to DB
//Select data from Table based on item selection
$query="SELECT partno FROM Table";
$params = array($_REQUEST['query']);
$results = sqlsrv_query($conn, $query, array(), array( "Scrollable" => 'static' ));
if($results===false)
{ die( FormatErrors( sqlsrv_errors() ) ); }
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<option>".$row['partno']."</option>";
}
exit;
?>
非常感谢帮助。谢谢。