簡體   English   中英

如何根據 php 中 combobox 中的選定值在數據庫中的文本框中顯示該值?

[英]How can I display value in textbox from database that value based on selected value from combobox in php?

我想在數據庫的文本框中顯示值,但該值基於 combobox 中的選定值。 以下是示例代碼,但我知道如何在 jquery 數組中使用數據庫中的值

<html>
<head>
</head>
<body>
<form id="taxDetails">
    <select name="ItemCode" onchange="setTax(document.Form, this.value);">
    <option value="">Select an Item from List...</option>
    <option value="AB">Pen</option>
    <option value="BC">Book</option>
    <option value="ON">Note book</option>
    </select>
    <input name="showtax" type="text" id="showtax" value="<show-value-of-tax-here" />
</form>

<script>
var taxCodes = {
    'AB': 5,
    'BC': 12,
    'ON': 13
};
var form = document.getElementById('taxDetails');
form.elements.ItemCode.onchange = function () {
    var form = this.form;
    form.elements.showtax.value = taxCodes[this.value];
};
</script>
</body>
</html>

以下是我要使用的查詢

$q= mysqli_query($connect,"select ItemCode, UnitPrice from oitm");

我確實知道如何在以下代碼中使用數據庫中的值

<script>    
var taxCodes = {
        'AB': 5,
        'BC': 12,
        'ON': 13
    };

我想要的是 select ItemCode 然后從上面的查詢中在文本框中顯示 UnitPrice

請任何人都可以幫助我

使用表單並將 GET HTTP 請求提交到 PHP 文件(在這種情況下是您的文件),該文件將從數據庫中獲取數據,使用表單標簽中的 action 屬性,如下所示:

<form method="get" action="your_file.php">

在文件的頂部,您可以這樣做:

$itemCode = $_GET['itemCode'];

$query = mysqli_query($connect,"select ItemCode, UnitPrice from oitm where ItemCode = '$itemCode");

$row = myqsli_fetch_array($query);

$price = $row['UnitPrice'];

並將輸入值屬性更改為:

<input name="showtax" type="text" id="showtax" value="<?php echo if(isset($price)) echo $price ?>" />

另一種解決方案是使用 AJAX,您可以使用 jquery 發出簡單的 ajax 請求。 希望這可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM