嗨,下面的代码有效,代码对类别的作用只是用户选择的下拉菜单,Length
但当用户选择长度时,它应该对数据库进行 ajax,然后无论价格如何用户从下拉列表中选择的那个长度应该在什么时候显示price
。
SELECT price FROM product WHERE
category=:dropdownval1 AND
type=:dropdownval2 AND prodID=:id
到目前为止,我所知道的是 ajax 应该运行上面的查询,但我不知道如何让 ajax 运行这个查询,到目前为止我已经尝试了 2 种不同的代码,但它们都没有工作。有人可以直接指出我的正确方向。
我知道通过 php 获取价格的 ajax,然后在回调中使用 jquery 更改价格
<form>
<tr>
<td width="160">Price:</td>
<?php
dbconnect();
$stmt2 = $conn->prepare("SELECT Length, price FROM Product WHERE ProdID=:id LIMIT 1");
$stmt2->bindParam('id',$id);
$stmt2->execute();
$i = 0;
foreach ($stmt2->fetchAll(PDO::FETCH_ASSOC) as $row2) {
if ($i == 0) {
echo '<td>'.$row2['price'].'</td>';
}
}
?>
<tr>
<td>Category</td>
<td>
<select name="Category">
<?php
dbconnect();
$stmt = $conn->prepare("SELECT Category FROM Product WHERE ProdID=:id GROUP BY Category");
$stmt->bindParam('id',$id);
$stmt->execute();
$i = 0;
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row ) {
if ($i == 0) {
$dropdownval1 = $row['Category'];
echo '<option SELECTED value="'.$row['Category'].'">'.$row['Category'].'</option>';
}
else {
echo '<option value="'.$row['Category'].'">'.$row['Category'].'</option>';
}
$i++;
}
?>
</select>
</td>
</tr>
<tr>
<td width="160">Length:</td>
<td>
<select name="length">
<?php
dbconnect();
$stmt3 = $conn->prepare("SELECT Length, Price FROM Product WHERE ProdID=:id AND Category=:dropdownval1");
$stmt3->bindParam('id',$id);
$stmt3->bindParam('dropdownval1',$dropdownval1 );
$stmt3->execute();
$i = 0;
foreach ($stmt3->fetchAll(PDO::FETCH_ASSOC) as $row3 ) {
if ($i == 0) {
echo '<option SELECTED value="'.$row3['Hair_Length'].'">'.$row3['Hair_Length'].'</option>';
}
else {
echo '<option value="'.$row3['Hair_Length'].'">'.$row3['Hair_Length'].'</option>';
}
$i++;
}
?>
</select>
</td>
</form>