猿问

如何将值从 mysql 回显到选项形式

我的服务页面中有两个下拉/选择输入,一个是服务类型,另一个是每个服务的价格,每个服务都是从 mysql 发送的如何使价格的值自动更改为连接到的任何数据服务类型需要什么例如选择,写一本书它会从数据库中显示它的价格


当我用 jquery 尝试它时,我不能给出任何选项值,因为它都来自 db


<label style="margin-left:20px; padding: 0 20px;

    margin-bottom: 39px;">what we do for you  </label><select name="topic"   Required="Required">

<?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);

}

# Set encoding to match PHP script encoding.

mysqli_set_charset($connection, 'utf8');

$query = "SELECT * FROM services";

$select_service = mysql_query($query);

while ($row = @mysql_fetch_assoc($select_service)) {

echo "<option value=\"".$row['id']."\">".$row['title']."</option>\n  ";

}

?>

</select><br>


<label style="    margin-left: 136px;

    margin-bottom: 30px;     padding: 0 20px;">price</label><select disabled name="topic"   Required="Required">

<?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);

}

# Set encoding to match PHP script encoding.

mysqli_set_charset($connection, 'utf8');

$query = "SELECT * FROM services";

$select_service = mysql_query($query);

while ($row = @mysql_fetch_assoc($select_service)) {

    $service_prise = $row['prise'];

    $service_content = $row['description'];

echo "<option >".$row['description']."</option>\n  ";

}

?>

</select>

<br>


撒科打诨
浏览 133回答 1
1回答

暮色呼如

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