1 回答
TA贡献1853条经验 获得超9个赞
您可以使用jquery&ajax在 select.i,e 更改时自动从数据库中获取值:
您的 jquery 将如下所示:
$("select[name='topic']").on("change",function(e) {
e.preventDefault();
//getting selected value
var topic=$(this).val();
console.log(topic);
$.ajax({
type: 'POST',
url: 'yourpagename.php',
data: {'topic': topic },//<-passing value to php
success: function(php) {
$("#price").html(php);//<--response will show in div with id=price
}
});
});
您的 php 页面:
<?php
// Make the connection:
$connection = mysqli_connect ('localhost', '#', '#', '#');
// If no connection could be made, trigger an error:
if ($connection->connect_error)
{
die("Database selection failed: " . $connection->connect_error);
}
$topic=$_POST['topic'];//<-getting value which is passed from ajax
# Set encoding to match PHP script encoding.
mysqli_set_charset($connection, 'utf8');
//here column name will be name of coulmn which you need to compare
$query = "SELECT * FROM services where columnname = '".$topic."'";
$select_service = mysqli_query($connection,$query);
echo '<label style=" margin-left: 136px;
margin-bottom: 30px;padding: 0 20px;">price</label><select disabled name="topic1" Required="Required">';
while ($row = mysqli_fetch_assoc($select_service)) {
$service_prise = $row['prise'];
$service_content = $row['description'];
echo "<option >".$row['description']."</option>\n ";
}
echo "</select>";
?>
同样在您当前的 php 页面中添加一个<div></div>.ie :
<div id="price">//<--Inside this div response from ajax will get display
<label style=" margin-left: 136px;
margin-bottom: 30px; padding: 0 20px;">price</label><select disabled name="topic" Required="Required">
>..
...
</select>
</div>
- 1 回答
- 0 关注
- 132 浏览
添加回答
举报