0

I am trying to filter state, zones of Nepal using ajax. I have got them in MySQL database.

I have populated zone select box initially, it is okay.

<select class="district col-md-3 center-block" id="zone">
<?php 
    while ($res = mysqli_fetch_assoc($result_zones)) {
        echo "<option value='".$res['name']."'>".$res['name']."</option>";
    }
?>
</select>

Now What I am trying is when user changes zone, I need all districts associated with this zone from districts table.

What I have done is here:

Ajax call

<script>
        $(document.body).on("change","#zone",function(){
            //console.log(this.value);
            $.ajax({
                dataType : 'json',
                async: false,
                url:'process.php?zone_id=' + this.value,
                complete: function (response) {
                    var result = JSON.stringify(response.data);//parsing status from response received from php
                    console.log(result);
                }
               });
        });
</script>

process.php

<?php 
    if (isset($_GET['zone_id'])) {
      $arr =array();
      $zone_id=$_GET['zone_id'];
      $i=0;
      $result = mysqli_query($dbconfig,"SELECT * FROM districts WHERE zone_id = '$zone_id'");
      while ($row = mysqli_fetch_assoc($result)) {
        $arr[$i] = $row;
        $i++;
      }
      // preparing correct format for json_encode
      header('Content-type: application/json'); 
      echo json_encode(array('data' => $arr)); //sending response to ajax
    }
 ?>

When I go through url http://localhost/nepal-locations/process.php?zone_id=8, I'm getting data as I need like below:

{"data":[{"id":"42","name":"Mustang","zone_id":"8"},{"id":"43","name":"Myagdi","zone_id":"8"},{"id":"44","name":"Parbat","zone_id":"8"},{"id":"45","name":"Baglung","zone_id":"8"}]}

But I'm getting data empty in console.

{data: []}
data
:
[]

What might be wrong I'm doing?

Update: output console.log(response) enter image description here

9
  • what's the console.log output if you do var result = response.data; without the json stringify ? Commented Jul 18, 2017 at 4:22
  • @Bdloud It's undefined Commented Jul 18, 2017 at 4:24
  • And a console.log of response ? Commented Jul 18, 2017 at 4:25
  • 1
    echo "<option value='".$res['name']."'>".$res['name']."</option>"; may be you need id instead of name in option value Commented Jul 18, 2017 at 4:26
  • 1
    @SureshPokharel have you check url? You need to pass ID while you are passing name in ajax. CHeck my previous comment, It will help you Commented Jul 18, 2017 at 4:39

2 Answers 2

3

pass in json encode $arr instead of "data"=>$arr

echo json_encode($arr);

becouse $arr is also an array

Sign up to request clarification or add additional context in comments.

Comments

2

You are getting empty array because you are passing name instead of id

change your <select> as below:

<select class="district col-md-3 center-block" id="zone">
<?php 
    while ($res = mysqli_fetch_assoc($result_zones)) {
        echo "<option value='".$res['id']."'>".$res['name']."</option>"; //<-----Add id instead of name
    }
?>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.