0

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;
  }
}

TrickStar
  • 229
  • 4
  • 19
Jaz
  • 55
  • 8
  • 1
    Instead of write php code in jQuery, You should call Ajax. – Madhusudan Oct 29 '18 at 05:32
  • 1
    [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Jonathan Lonowski Oct 29 '18 at 05:36
  • 1
    I agree with Jonathan. It looks like you want: **3. Echo the data directly to JavaScript** of the accepted answer. – mickmackusa Oct 29 '18 at 09:01
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – mickmackusa Oct 29 '18 at 09:02

0 Answers0