从数据库获取数组值(php)

时间:2018-10-29 05:13:44

标签: javascript php select

我计划从数据库中获取所有数组值以执行选择类别。一切都很好,而且看起来很简单,但是在用变量$address回显它时遇到了麻烦。我也做了硬编码的方法来检查js代码中是否有错误,并且工作正常。是错误还是我错过了重要的事情?

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

0 个答案:

没有答案