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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里