Reputation: 19
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
Upvotes: 0
Views: 432
Reputation: 2065
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'].'
anddata-price2='.$prow['Price2'].'
Step 2 : Use jquery
attr
to getdata-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.
Upvotes: 1
Reputation: 608
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
Upvotes: 0