dtx3006 2018-09-05 11:22
浏览 120
已采纳

使用下拉菜单从mysql表中选择值时填充html表单

I've table calles products_tbl

+-----+------------+----------------+-------+------+-----------+
|pr_id|product_name|product_category|Price1 |Price2|product_img|
+-----+------------+----------------+-------+------+-----------+
|1    |Apple       |fruits          |12     |15    |aimg.jpg   |
|2    |Orange      |fruits          |10     |11    |orimg.jpg  |
|3    |Iphone X    |Electronics     |900    |1025  |iphimg.jpg |
|4    |FJ-Eye Lens |Accessories     |20     |25    |fjimg.jpg  |
+-----+------------+----------------+-------+------+-----------+

I've grab product_name from table and put it in Select Option and when user choose value from select option it display it image and put it in img tag (this worked perfect).

I want when user select some product name from select option it displayed the price1 and price2 in 2 text input as the same as the image

products.php

$productQ = "SELECT * FROM products_tbl ORDER BY product_category ASC";
try { 
    $stmt2 = $db->prepare($productQ); 
    $stmt2->execute();
} catch(PDOException $ex) { 
    die("Failed to run membersQ: " . $ex->getMessage()); 
} 
$produtsrows = $stmt2->fetchAll();

echo"<select name='dropdown' id='dropdown' required>
        <option selected disabled>Choose</option>";
    $category = "";
    foreach($produtsrows as $prow): 
        if ($category != $prow['product_category']) {
            if ($category != "") {
                echo "</optgroup>";
            }
            $category = $prow['product_category'];
            echo "<optgroup label=".$prow['product_name'].">";
        }
        echo"   <option value='imgs/".$prow['product_img']."'>".$prow['product_name']."</option>";
    endforeach;
    echo "</optgroup>
</select>
<input type='text' name='p1' id='p1' value=''>
<input type='text' name='p1' id='p2' value=''>

<img id='image' src='' alt=''>";

and JavaScript

<script type="text/javascript">
$(function(){
    $( '#dropdown' ).change(function(){
        $( '#image' ).attr( 'src', $( this ).val() + '' );
        $( '#p1' ).attr( 'value', $( this ).val() + '' );
        $( '#p2' ).attr( 'value', $( this ).val() + '' );
    })

});

</script>

when I select any value from select option it display the name of the image in the textbox rather than price1 and price2

how can I display the price1 and price2 rather than image name

  • 写回答

2条回答 默认 最新

  • douxin2011 2018-09-05 11:34
    关注

    You can apply new attribute in order to get more then one value from drop down list.

    Solution :

    Step 1 : Add data-price1='.$prow['Price1'].' and data-price2='.$prow['Price2'].'

    Step 2 : Use jquery attr to get data-price* values

    Example :

    .php File :

    $productQ = "SELECT * FROM products_tbl ORDER BY product_category ASC";
    try { 
        $stmt2 = $db->prepare($productQ); 
        $stmt2->execute();
    } catch(PDOException $ex) { 
        die("Failed to run membersQ: " . $ex->getMessage()); 
    } 
    $produtsrows = $stmt2->fetchAll();
    
    echo"<select name='dropdown' id='dropdown' required>
            <option selected disabled>Choose</option>";
        $category = "";
        foreach($produtsrows as $prow): 
            if ($category != $prow['product_category']) {
                if ($category != "") {
                    echo "</optgroup>";
                }
                $category = $prow['product_category'];
                echo "<optgroup label=".$prow['product_name'].">";
            }
            echo"   <option value='imgs/".$prow['product_img']."' data-price1='.$prow['Price1'].' data-price2='.$prow['Price2'].'>".$prow['product_name']."</option>";
        endforeach;
        echo "</optgroup>
    </select>
    <input type='text' name='p1' id='p1' value=''>
    <input type='text' name='p1' id='p2' value=''>
    
    <img id='image' src='' alt=''>";
    

    jQuery:

    $(function(){
        $( '#dropdown' ).change(function(){
            $('#image').attr( 'src', $( this ).val() + '' );
            $('#p1').attr( 'value', $(this).children('option:selected').attr('data-price1'));
            $('#p2').attr( 'value', $(this).children('option:selected').attr('data-price2'));
        })
    });
    

    Check below HTML code:

    $(document).ready(function () {
        $('#dropdown').change(function () {
            $('#p1').attr('value', $(this).children('option:selected').attr('data-price1'));
            $('#p2').attr('value', $(this).children('option:selected').attr('data-price2'));
        })
    });//jq
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    Change list : 
    <select id="dropdown">
        <option data-price1="12" data-price2="15">Apple</option>
        <option data-price1="10" data-price2="11">Orange</option>
        <option data-price1="900" data-price2="1025">Iphone X</option>
        <option data-price1="20" data-price2="25">FJ-Eye Lens</option>
    </select>
    <br />
    Price 1 : <input type="text" id="p1" />
    <br />
    Price 2 : <input type="text" id="p2" />

    I hope this will help.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加