I tried to write codes which display value in textbox based on value selected in combobox, but I have one problem I puted combobox and texbox in loop but only first row can display value of selected item in combobox where I want each row to display value based on value selected in its combobox Below are my codes
<?php
require_once('connection.php');
for($counter=0;$counter<=1;$counter++)
{
$queItem= mysqli_query($connect,"select * from oitm");
echo"<select name=\"segment_id\" class=\"form-control\" id=\"segment_id\" STYLE=\"width: 300px\" >";
echo"<option value=\"\" selected=\"selected\">Select a product</option>";
while($rowStockIn = mysqli_fetch_array($queItem))
{
$ItemCode2=$rowStockIn['ItemCode'];
$ItemName2=$rowStockIn['ItemName'];
$ItemPrice2=$rowStockIn['ItemPrice'];
echo "<option value=\"$ItemCode2\" data-itemprice = \"$ItemPrice2\">
$ItemCode2 $ItemName2";
}
echo"</select>";
?>
</span>
<?php
echo"<input id=\"title\" name=\"title\" type=\"text\" value=\"$ItemPrice2\"
><br/>";
}
?>
<script>
$(document).ready(function(){
$("#segment_id").on('change', function () {
var id = $(this).val();
var itemprice = $('option:selected',this).data('itemprice');
$("#title").val(itemprice);
});
});
</script>
I think I have to edit jQuery in order to allow each row to work
Please anyone can help me
Ok, well, if you are using a combo box then clearly the user should be able to select more than one option. Your first problem is that $itemCode2 in your while loop is just a variable not an array. Therefore, as you described in your issue you'll only ever get one value because it will be overwritten each time in the loop.
Make $itemCode2 an array - for example:
$itemCode2[] = $rowStockin['itemCode']
Then you should be able to output the various options that the user can select.
Also, be careful using select * in your queries. Try to be specific in your column selections. select * could leave your script exposed to a sql injection attack.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.