0

I need to retrieve an array from a PHP file which is a SQL query coming back in an array. I've encoded it in with json_encode(); and returned it.

Here is my JS file:

$(document).ready(function() {
$('#indicators').change(function() {
    $('#countries').fadeIn('slow');
    var indic_val = $(this).val();
    var countries = $.ajax({
        url: 'scripts/chart_handler.php',
        dataType: "json",
        type: 'POST',
        data: {'indicator' : indic_val},
        async:false,
        success: (function ( data ){
            console.log(data);
            $.each(data, function(i,key) {
                $('#countries').append('<option value="'+ key +'">'+ key +'</option>');
                    });
               })
         });
    });
});

I am getting inside success tag but data is coming back null. What do I have to use to get the data array?

Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

1

The answer to my problem was simply changing in the PHP script the 'returning or ending' line. It happened thanks to misunderstanding of AJAX.

Change:

return json_encode($array);

for:

echo json_encode($array);

0

You could try use getJSON

$.getJSON('scripts/chart_handler.php')
.done(function( data ) {
  $.each( data, function( i, item ) {

   $('#countries').append('<option value="'+ item +'">'+ item +'</option>');

  });
});

I if you are using FECH ASSOC in your query, you will need use item.namefield where namefield are the name of field you want insted of only item

You may use firebug (a firefox extencion) to debug and see item dom extruture

If you want to send something by get method do as below

$.getJSON('scripts/chart_handler.php',{indicator : indic_val})
.done(function( data ) {
  $.each( data, function( i, item ) {

   $('#countries').append('<option value="'+ item +'">'+ item +'</option>');

  });
});
Heberfa
  • 441
  • 3
  • 8
0

I think this could be caused by UTF8-BOM (Byte Order Mark). BOM is added to the beginning of the json string which will invalidate it.

Try convert your document to UTF8 without BOM and give it another go.

For further information, please see this other thread on SO: json_decode returns NULL after webservice call

Community
  • 1
  • 1
MrE
  • 1,124
  • 3
  • 14
  • 28
0

Use the following code

$('#ddlCountry').change(function(){

        $.ajax({                
            type:'POST',
            url:'<?php echo base_url(); ?>admin_in_action/GetDivisionByCountry.php',
            datatype:'json',
            data:{countryId:this.value},
            cache:false,
            success:function(result){
                $('#ddlDivision').get(0).options.length = 0;
                $('#ddlDivision').get(0).options[0] = new Option("--Select--", "0");

                $.map(result, function(item){
                    $('#ddlDivision').get(0).options[$('#ddlDivision').get(0).options.length] = new Option(item.DivisionName, item.DivisionID);
                });                

            },
            error:function(){}
        });            
    }); 
Md. Nazrul Islam
  • 2,809
  • 26
  • 31