I have this plan to get all array value from database to perform a selecting categories. All is fine and seems so easy but I get trouble in echo-ing it with variable $address
. I also did the hard coded way to check if theres error within the js codes and it works fine. Is it a bug or I just missed something important?
<select name="category" id="category" onChange="category(this.value);" >
<option value="Status">Status</option>
<option value="OrderDate">Order Date</option>
<option value="DeliveryArea">Delivery Area</option>
</select>
<select name="choice" id="choice">
<option name="choice" value=""> Select Category </option>
</select>
<script>
var Category ={
"Status":["Confirmed","Pending","blah"],
"OrderDate":["123","123","123"],
"DeliveryArea":[<?php
include 'conn.php';
$sql ="SELECT * from orders GROUP BY address ORDER BY address ASC;";
$query = mysqli_query($connection,$sql);
$num_row = mysqli_num_rows($query);
while($inside = mysqli_fetch_assoc($query)) {
$address = $inside['address'];
echo '"';
echo $address;
echo '",';
}?>
],
}
function category(value){
if(value.length==0) document.getElementId("choice").innerHTML="
<option></option>";
else {
var category_options="";
for(category_name in Category[value]){
category_options +="<option name='choice' value='"+Category[value][category_name]+"'>"+Category[value][category_name]+"</option>";
}
document.getElementById("choice").innerHTML =category_options;
}
}