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?

8
  • 1
    Have you used Firebug or similar to see if you're getting what you expected back off the Ajax request? Commented Apr 17, 2013 at 14:22
  • if the console.log(data) comes back null, then check that your php script is sending valid json to begin with. Commented Apr 17, 2013 at 14:22
  • 1
    Post your PHP code too. Commented Apr 17, 2013 at 14:23
  • If the data is'nt being sent, would'nt it be a great idea to include the function that sends the data, and not the one that receives it ? Commented Apr 17, 2013 at 14:24
  • try this: var data = $.parseJSON(data); Commented Apr 17, 2013 at 14:26

4 Answers 4

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

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

Comments

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

Comments

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>');

  });
});

3 Comments

Seems like the OP needs to do a POST request.
It still does a GET request, not a POST request like in the question.
@Heberfa: sure, but that data is represented as query string parameters in the request, not as POST data.
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(){}
        });            
    }); 

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.