I'm having a hard time figuring this out.
This is my html with drop down list, it's working as it should. But the thing is, I don't have a clue on how to output the Stock data that's in Stock Column of itemprofile table in a way that every time I pick a different product on the drop down list, Stock value should also change accordingly.
<html>
<body>
<form>
<select name="product" />
<?php
$mysqli = new mysqli("localhost", "root", "","bsystem");
$results = $mysqli->query("SELECT * FROM itemprofile");
if ($results) {
while($obj = $results->fetch_object())
{
echo '<option value="'.$obj->productname.'">'.$obj->productname.'</option>';
}
}
?>
</select>
Available Stock: <?php NO CLUE WHAT TO DO HERE?>
</form>
</body>
</html>
The content of my database:
3 columns: id, productname, stock
1 item1 50
2 item2 30
3 item3 10
Hope you guys can help me out figuring this out. Thanks!
Edit: my code so far.
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
</head>
<body>
<select id="selectItem">
<option>-- Select item --</option>
<?php
$mysqli = new mysqli("localhost", "root", "","bsystem");
$results = $mysqli->query("SELECT * FROM itemprofile");
$itemsStock = array(); # create an array where you will keep your stock values for each item
if ($results) {
while($obj = $results->fetch_object()){
echo '<option value="'.$obj->productname.'">'.$obj->productname.'</option>';
$itemsStock[$obj->id] = $obj->stock; # fill array with stock values
}
}
?>
</select>
Available stock: <div id="stockValue"></div>
<script>
var stockValues = <?php echo json_encode($itemsStock);?>; // transfer array that contains item stock values from php to the javascript array
$("#selectItem").change(function(){ // on every DropDown change
var ItemID = $(this).val(); // get selected item id
console.log(ItemID);
var ItemStockValue = stockValues[ItemID]; // search stock value in your array of stock values by using ItemID
$("#stockValue").html(ItemStockValue); // update Available stock for selected item
});
</script>
</body>
</html>
NO CLUE WHAT TO DO HEREAnd how we should know what to do?