1

I am trying to get data to display in a table. I don't know what I am doing wrong, but when I get the data from my page it is an array of single characters. I could parse this myself but would prefer to know what I am doing wrong.

I have this php to get the data:

function BuildViewerCombo($autocomplete) {

    $wholeNumberCombo = array();
    $dbhandle = DB_Connect();       

    $result = QueryForward($dbhandle, SQL_WholeNumbersPartial($autocomplete));

    while($wholeNumber = sqlsrv_fetch_array($result))
        {
            $wholeNumberCombo[] = array($wholeNumber['DocumentNbr'] => 'Number', $wholeNumber['DocumentRevision'] => 'Revision');
        }   


    //close the connection
    sqlsrv_close($dbhandle);

    return $wholeNumberCombo;
}

Which is called from this page

<?PHP 
    include "Scripts/DB_Functions.php5" ;
    include "Scripts/SQL_Viewer.php5" ;

    $wholeNumber = $_GET['wholeNumber'];
    echo json_encode(BuildViewerCombo($wholeNumber));
?>

Which gets loaded from this function

    function toggleDropdown()
    {           

        var wholeNumberData 


        var wholeNumber = document.getElementById('WholeNumber').value;

        if (wholeNumber != '') {
            wholeNumberData = GetData('wholeNumber', wholeNumber);

            var table = document.getElementById("wholeNumberDropdown");

            alert ('WN = ' + wholeNumberData.length);
            alert (wholeNumberData);
            for (var i in wholeNumberData) {
                alert(wholeNumberData[i]); 
            }

            }
        else {
            alert("Please enter a whole number.");
        }

    }

By calling this function:

function GetData(getType, param) {

    var http = new XMLHttpRequest();

    http.open("GET", 'ViewerWholeNumbers.php?wholeNumber=' + param, false);
    http.setRequestHeader("Content-type","application/json");
    http.onload = function() {
    }
    http.send('wholeNumber=' + param);
    return http.responseText;
}

The data that gets returned is:

[{"SS3999":"Number","A":"Revision"},{"SS3999":"Number","11":"Revision"},
{"SS3999":"Number","11":"Revision"},{"SS3999":"Number","11":"Revision"},
{"SS3999":"Number","":"Revision"},{"SS3999":"Number","11":"Revision"},
{"SS3999":"Number","":"Revision"},{"SS3999":"Number","11":"Revision"},
{"SS3999":"Number","11":"Revision"},{"SS3999":"Number","A":"Revision"},
{"SS3999":"Number","11":"Revision"},{"SS3999":"Number","A":"Revision"},
{"SS3999":"Number","11":"Revision"},{"SS3999":"Number","A":"Revision"},
{"SS3999":"Number","":"Revision"}]  

But alert ('WN = ' + wholeNumberData.length); returns 546 and when I try to loop through the array I get a single character for each element instead of the values.

7
  • you can use json_decode() function which is convert json to array Commented Jun 22, 2015 at 16:47
  • Yeah I agree, php.net/manual/en/function.json-decode.php Commented Jun 22, 2015 at 16:55
  • Returned data is turned upside down. It should be {"Number":"SS3999"... and not the other way around Commented Jun 22, 2015 at 16:56
  • @Sel: json_decode is a php function and I need the data in javascript. Commented Jun 22, 2015 at 17:00
  • 1
    You need JSON.parse() to parse the string and convert it to an array of objects in javascript. Commented Jun 22, 2015 at 17:05

1 Answer 1

3

First off, your associative array is flipped. You need to change

array($wholeNumber['DocumentNbr'] => 'Number', $wholeNumber['DocumentRevision'] => 'Revision'); 

to

array('Number' => $wholeNumber['DocumentNbr'], 'Revision' => $wholeNumber['DocumentRevision']);

You need that in order to access the elements of the JSON. Then, in your loop, you would use wholeNumberData[i].Number to get the number and wholeNumberData[i].Revision to get the revision.

Update:

As @jeroen pointed out, you need JSON.parse() to convert the return string to JSON. In your GetData function replace your return with this:

return JSON.parse(http.responseText);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I flipped it. However I get 'undefined' when trying to access a member like this: alert ('Number = ' + wholeNumberData[1].Number);

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.