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

时间:2018-09-05 11:22:02

标签: javascript php mysqli pdo

我已在表中调用 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  |
+-----+------------+----------------+-------+------+-----------+

我从表中获取了 product_name 并将其放在 Select Option 中,当用户从select选项中选择值时,它将显示图像并将其放入img标签(此效果很好)。

我想要当用户从选择选项中选择一些产品名称时,它在价格输入1和价格2中以与图片相同的方式在2个文本中显示

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=''>";

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>

当我从选择选项中选择任何值时,它将在文本框中显示图像的名称,而不是price1和price2

如何显示price1和price2而不是图像名称

2 个答案:

答案 0 :(得分:0)

您可以应用新属性,以便从下拉列表中获取一个以上的值。

  

解决方案:

     

步骤1:添加data-price1='.$prow['Price1'].'data-price2='.$prow['Price2'].'

     

第2步:使用jquery attr获得data-price*

示例:

.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']."' 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'));
    })
});

检查以下HTML代码:

$(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" />

我希望这会有所帮助。

答案 1 :(得分:0)

Bind the attribute in option and change the script..

<select id="slct" onchange="dropdownhange(this)">
<option value="1" data-cust="a"> 1 </option>
<option value="2" data-cust="b"> 2 </option>
<option value="3" data-cust="c"> 3 </option>
</select>

function dropdownhange(obj){
  console.log($(obj).val());
  console.log($(obj).children('option:selected').attr('data-cust'));
}

source code