0

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 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

I think this refers to the dropdown & you bind the data-price & data-price2 to option. Maybe you need to check the option: selected attribute value.
it return undefined in the 2 textbox
I think you need something $(this).children('option:selected').attr('data-price1''));
@HaydeerHussien, applied HTML working code above, please check
Dear @saAction the HTML example worked perfectly, but when I'm trying it to my code its not showing any results
|
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'));
}

https://codepen.io/pixel-lab/pen/zJdqvv?editors=1111

2 Comments

its working on console ? how about display it in the textbox ?
something like : $('#dropdown').change(function () { $('#p1').val($(this).children('option:selected').attr('data-price1')); $('#p2').val($(this).children('option:selected').attr('data-price2')); })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.