简体   繁体   English

如何根据 php 中 combobox 中的选定值在数据库中的文本框中显示该值?

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

I want to display value in textbox from database but the value based on selected value from combobox.我想在数据库的文本框中显示值,但该值基于 combobox 中的选定值。 Below are sample codes but I do know how to use value from database in array of jquery以下是示例代码,但我知道如何在 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>

Below is my query I want to use以下是我要使用的查询

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

I do know how I can use the value from database in the following codes我确实知道如何在以下代码中使用数据库中的值

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

What I want is to select ItemCode and then display UnitPrice in textbox from above query我想要的是 select ItemCode 然后从上面的查询中在文本框中显示 UnitPrice

Please anyone can help me请任何人都可以帮助我

Use the form and submit a GET HTTP request to a PHP file (your file in this situation) which will fetch the data from the database, use the action attribute in the form tag like so:使用表单并将 GET HTTP 请求提交到 PHP 文件(在这种情况下是您的文件),该文件将从数据库中获取数据,使用表单标签中的 action 属性,如下所示:

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

in the top of your file you can do this:在文件的顶部,您可以这样做:

$itemCode = $_GET['itemCode'];

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

$row = myqsli_fetch_array($query);

$price = $row['UnitPrice'];

And change the input value attribute to this:并将输入值属性更改为:

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

Another solution is to use AJAX, you can use jquery to make a simple ajax request.另一种解决方案是使用 AJAX,您可以使用 jquery 发出简单的 ajax 请求。 hope this helps you.希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据来自form_for select的所选值显示空文本框 - Display empty textbox based on selected value from form_for select 如何根据选定的值/在数据库的文本区域中显示值? - How can I show value in the textarea from the database /based on the selected value? 根据从下拉列表中选择的值显示文本框 - display textbox as per the value selected from dropdown 更改组合框值时从数据库动态更新文本框值 - dynamically update textbox value from database when combobox value is changed 如何在PHP中从数据库中选择一个选定的值 - How to select a selected value from database in PHP 如何从多个下拉列表中显示已选择的数据库值的值 - How to display value of the already selected database value from the multiple dropdown 如何根据数据库中的值在下拉列表中选择一个值 - How to make a value in dropdown selected based on value from database 单击按钮并用php数据库中的数据填充组合框,然后从数据库中加载选定的值 - filling combobox with data from php database on button click and load selected value from database 如何根据其他组合框的选定值填充组合框? - How can i populate a combobox depending on the selected value of a different combobox? 如何显示基于选定值的搜索表单 - How can I display a search form based on a selected value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM