im trying to echo out a variable that is written from php using javascript. Well basically I have a drop down menu and what I am trying to do is echo out the number of stocks of the item selected by the user. Here is what I've got so far.
<select id="selectItem" onchange="stock(this)">
<?php
--- connection here ----
$itemsStock = array();
$result = mysql_query("SELECT * FROM itemprofile");
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$stock = $row['stock'];
echo '<option value="'.$row['productname'].'" id="'.$row['id'].'">'.$row['productname'].'</option>';
$itemsStock[$id] = $stock;
}
?>
</select>
Available stock: <div id="stockRemaining"></div>
<script type="text/javascript">
var json_arr = <?php echo json_encode($itemsStock);?>;
var stockValues = JSON.parse(json_arr);
function stock(sel)
{
var itemID = sel.value;
console.log(itemID); // debug to see that you are getting the correct value
document.getElementById('stockRemaining').innerHTML = stockValues[itemID];
}
</script>
The convertion of array from php to jquery is fine, i tried echoing it out and it works fine, my problem is the stock() function, I think im doing something wrong on the action inside it cuz everytime I click a different item from the drop down menu, it doesnt show anything at all. Im pretty new to Jscript hope you guys can help me out. Thanks!