1

I want to display value in textbox from database but the value based on selected value from combobox. Below are sample codes but I do know how to use value from database in array of 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

Please anyone can help me

Schadro
  • 63
  • 6
  • Welcome to SO. Great job on your first question. You want to use the values from the HTML form, specifically the `UnitPrice` value in your SQL query? – Emiel Zuurbier Apr 25 '20 at 17:09
  • Or do you want to output a PHP value to use in JavaScript? – Emiel Zuurbier Apr 25 '20 at 17:14
  • Sorry the value from in the html select option will come from in database where AB,BC and ON are ItemCodes from database and 5,12 and 13 (UnitPrice) are also value from database when you select Pen then 5 in unitprice in textbox – Schadro Apr 25 '20 at 17:17

1 Answers1

0

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 :

<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. hope this helps you.

Shakir El Amrani
  • 331
  • 2
  • 16
  • Hi chou500, I tried that codes but when I select an item it does not display anything, how can I use value from the query in the following array, I do know how I can use like $ItemCode and $price as 'AB':5 – Schadro Apr 25 '20 at 18:38
  • It displays nothings because your missing to send the form on the select event, please see https://stackoverflow.com/questions/7231157/how-to-submit-form-on-change-of-dropdown-list. – Shakir El Amrani Apr 25 '20 at 18:56
  • but the problem is, that loads the page i wanted to display unit price without loading the page – Schadro Apr 25 '20 at 21:45
  • Use ajax request to do that, you can make an ajax request with jquery very easily. – Shakir El Amrani Apr 25 '20 at 21:49