为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
撒科打诨 2021-11-13 16:33:51
我的服务页面中有两个下拉/选择输入,一个是服务类型,另一个是每个服务的价格,每个服务都是从 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>
查看完整描述

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>


查看完整回答
反对 回复 2021-11-13
  • 1 回答
  • 0 关注
  • 132 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信