我是jQuery的新手,並且遇到了無法發佈源自依賴動態下拉列表的值的問題。我不能張貼從動態依賴項下拉列表中的值
這是我寫的代碼:
的index.php
<form name="form1" action="test.php" method="post">
<table>
<tr>
<td>
<select id="branddd" onchange="change_brand()" required>
<option disabled="disabled" selected="selected" style="color:gray" required >brand</option>
<?php
$res=mysqli_query($link,"select * from brand");
while($row=mysqli_fetch_array($res))
{
?>
<option name="brand" value="<?php echo $row["id"];?>"><?php echo $row["name"];?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>
<div id="model">
<select name="model">
<option required>model</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="submit"></td>
</tr>
</table>
這是我寫從MySQL數據加載到第二個下拉框中的JavaScript:
<script type="text/javascript">
function change_brand()
{
var xmlhttp=new XMLHttpRequest() ;
xmlhttp.open("GET","ajax.php? brand="+document.getElementById("branddd").value,false) ;
xmlhttp.send(null) ;
document.getElementById("model").innerHTML=xmlhttp.responseText ;
}
</script>
ajax.php:
<?php
$link=mysqli_connect("localhost","root","");
mysqli_select_db($link,"test_db");
$brand=$_GET["brand"];
if($brand!="")
{
$res=mysqli_query($link,"select * from model where brand_id=$brand");
echo "<select>";
while($row=mysqli_fetch_array($res))
{
echo "<option>"; echo $row["name"]; echo "</option>";
}
echo "</select>";
}
?>
你說你正在嘗試POST,但你正在使用GET。要使用jquery進行POST,你可以使用[$ .post](https://api.jquery.com/jquery.post/) – user2896976
你必須有你的第一個選擇名稱 – yogesh84
謝謝我忘了我只是添加它,但我得到id值然後名字值 – Mouhssine