![](/img/trans.png)
[英]How can I display value in textbox from database that value based on selected value from combobox in php?
[英]How can I display value in each textbox in loop that the value based on the value selected in its combobox?
我嘗試編寫代碼,根據 combobox 中選擇的值在文本框中顯示值,但我有一個問題我將 combobox 和 texbox 放入循環中,但只有第一行可以在 ZA8284521647549D6ECZBB008 中顯示所選項目的值在其 combobox 中選擇的值下面是我的代碼
<?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>
我想我必須編輯 jQuery 以允許每一行工作
請任何人都可以幫助我
好的,好吧,如果您使用的是組合框,那么顯然用戶應該能夠使用 select 多個選項。 您的第一個問題是 while 循環中的 $itemCode2 只是一個變量而不是數組。 因此,正如您在問題中描述的那樣,您只會得到一個值,因為每次循環中都會覆蓋它。
使 $itemCode2 成為一個數組 - 例如:
$itemCode2[] = $rowStockin['itemCode']
然后你應該可以output的各種選項,用戶可以select。
另外,在查詢中使用 select * 時要小心。 嘗試在您的列選擇中具體化。 select * 可能會使您的腳本暴露於 sql 注入攻擊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.