Derple
Derple

Reputation: 863

AJAX value from PHP returned as undefined with JSON

I have an AJAX script updating a database via PHP.

I am then trying to return two variables back to the AJAX success function.

Currently, when alerting the data returned they are showing UNDEFINED.

When I return the JSON without stating the part of the array I require, the array displays in full. But only when I state specifically which value from the array I wish to use, I seem to get undefined on both values.

How should I manage these values returned from the PHP?

The AJAX

success: function(data) {
    $('#'+data.toUpdate).html(data.quant);
    $('#'+data.toUpdate).addClass('updated_grn');
    alert('quant:' + data.quant + '\nid:' + data.toUpdate);  
}

The PHP

if ($query) {
    echo json_encode(array("toUpdate" => $toUpdate, "quant" => $quant));
}

The Result

Ajax returns Undefined

When I alert(data) this is returned:

Returned data

The full php

$itemid   = ($_POST['itemid']);
$quant    = ($_POST['quant']);
$toUpdate = ($_POST['toUpdate']);

$sql = "UPDATE items_list
        SET `stock_level` = '$quant'
        WHERE item_id = '$itemid'";

$query = mysql_query($sql);

if ($query) {
    echo json_encode(array("toUpdate" => $toUpdate, "quant" => $quant));
}

The full AJAX

$.ajax({
    type:     'POST',
    url:      url,
    dataType: 'html',
    data: {
        itemid:   itemid,
        quant:    quant,
        toUpdate: toUpdate
    },
    beforeSend: function() {
        $('#'+id+'_num')
            .html("<img src='xxxxxxx.com/home/secure/images/gif/ajax-loader.gif'></img>");
    },
    success: function(data) {
        alert(data);
        //  $('#'+data.toUpdate).html(data.quant);
        //  $('#'+data.toUpdate).addClass('updated_grn');
        //  alert('quant:' + data.quant + '\nid:' + data.toUpdate);
    }
});

Upvotes: 1

Views: 1492

Answers (3)

wpjr
wpjr

Reputation: 11

FYI. I found this problem when using WordPress (4.3.1) and tried to load json data to D3.json function inside WordPress. json_encode(some_array) did not work but returned undefined. It returned a single integer value but not any array. UTF-8 check or anything else did not help.

Then incidently I closed the php ajax function by wp_die() command, it was missing there. And voila!, this suddenly works, json_encode returns array fine.

Hopefully this helps someone.

Upvotes: 0

Darren
Darren

Reputation: 13128

Just change dataType: 'html' to dataType: 'json' :)

Upvotes: 2

Ladislav Gallay
Ladislav Gallay

Reputation: 478

Make sure that $query is true. I would also do die() so nothing else is in output:

 if ($query) {
    die(json_encode(array("toUpdate" => $toUpdate, "quant" => $quant)));
}

You can always check what you get from PHP:

success: function(data) {
        alert(data);
        $('#'+data.toUpdate).html(data.quant);
        $('#'+data.toUpdate).addClass('updated_grn');
        alert('quant:'+data.quant+'\nid:'+data.toUpdate);  
}

Upvotes: 0

Related Questions