php - How can I display value in each textbox in loop that the value based on the value selected in its combobox?

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
Answer
Solution:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.