1

I have a case in my code, the result does not appear or work, the city form select is still blank. Here is the HTML:

<div class="form-group">
  <label>Province</label>
  <select class="form-control" name="province" id="province">

    <?php foreach($state_data->data as $sd){ ?>
    <option id="<?php echo $sd->code; ?>" value="<?php echo $sd->code; ?>" <?php echo (isset($this->session->userdata))?(($this->session->userdata('session_state_code') == $sd->code)?"selected":""):"";?>>
      <?php echo $sd->name ;?>
    </option>
    <?php } ?>
  </select>
</div>
<div class="form-group" id="city_section">
  <label>City</label>
  <select class="form-control" name="city" id="city">

    <?php foreach($city_data->data as $cd){ ?>
    <option value="<?php echo $cd->code; ?>" <?php echo (isset($this->session->userdata))?(($this->session->userdata('session_city_code') == $cd->code)?"selected":""):"";?>>
      <?php echo $cd->name ;?>
    </option>
    <?php } ?>
  </select>
</div>

This the javascript to get data from the controller:

$('#province').on('change', function() {
      var id = $(this).val();;
      $.ajax({
        type: "POST",
        dataType: 'json',
        url: "<?php echo base_url() ?>edit_profile/get_city",
        data: {
          id: $(this).val()
        },
        success: function(data) {
          $('select#city').html('');
          for (var i = 0; i < data.length; i++) {
            $("<option />").val(data[i].code)
              .text(data[i].name)
              .appendTo($('select#city'));
          }
        }
      });
    }

and this the controller to get data from API:

public function get_city(){
  $m_url = URL_API;
  $function = "get_location";
  $i = $_REQUEST['id'];
  $url = $m_url . "/" . $function . "?" . "t=city" . "&" . "i=".$i;
  $json_data = file_get_contents($url);
  $data = json_decode($json_data);  
}
2
  • any error u get in console ? Commented May 4, 2016 at 7:37
  • no, i dont get @ManinderpreetSingh Commented May 4, 2016 at 7:44

3 Answers 3

1

Your function does't return anything to ajax you need to echo json_decode

public function get_city(){
        $m_url = URL_API;
        $function = "get_location";
        $i = $_REQUEST['id'];
        $url = $m_url . "/" . $function . "?" . "t=city" . "&" . "i=".$i;
        $json_data = file_get_contents($url);
        echo $data = json_decode($json_data);// use echo here
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I add echo but still same @Saty
0

You dont need json_decode(), just print $json_data. Because, it's already returns as string by file_get_contents().

public function get_city(){

    $m_url = URL_API;
    $function = "get_location";
    $i = $_REQUEST['id'];
    $url = $m_url . "/" . $function . "?" . "t=city" . "&" . "i=".$i;
    $json_data = file_get_contents($url);
    echo $json_data;


}

Convert returned data to JSON object. And then use it:

   success: function(data) {
      var jsonObj = JSON.parse(data);
      $('select#city').html('');
      for (var i = 0; i < jsonObj.length; i++) {
        $("<option />").val(jsonObj[i].code)
          .text(jsonObj[i].name)
          .appendTo($('select#city'));
      }
    }

3 Comments

I have add your advice but dont effect anything. but thanks. what about my ajax?
Convert the returned string to JSON object. You are missed it.
I have converted but still empty
0

Having echo $json_data; ,now try to put error callback in your Ajax so that you will see the error response of your request. From there you will have an idea what went wrong within the process.

error: function(err){
  console.log(err.responseText);
}

2 Comments

there is no error result/ response friend. thanks for your advice
Instead of data: { id: $(this).val() }, should be data: { 'id': id }, since you have var id= $(this).val() .

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.