1

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);
    }
});
5
  • Updated question with full code and image. Commented May 25, 2014 at 23:54
  • 2
    What happens if you change dataType: 'html' to dataType: 'json' Commented May 25, 2014 at 23:54
  • 4
    Change your AJAX dataType to json Commented May 25, 2014 at 23:56
  • 1
    oh wow. Don't i feel silly now. Thank you, if you answer i shall tick yours as correct, many thanks. @Darren Commented May 25, 2014 at 23:56
  • @StuartWickenden It's a pleasure :) I too ran into that issue once upon a time and felt like a complete idiot! Commented May 25, 2014 at 23:58

3 Answers 3

2

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

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

Comments

0

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

2 Comments

updated, the data returns in the array when alert(data). Illustrated in question.
Use jQuery.parseJSON like this: var obj = jQuery.parseJSON(data); alert(obj.quant);
0

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.

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.