Getting values ​​from a JSON array string

My .php code in the "fetchvalues.php" file looks like this:

echo json_encode(array($PostedDate.Places.$Company.$Designation.$ProjectDetails.$DesiredCandidate.$HRName.$HRContact.$Email));

This file is called by another file, and the calling function looks like this:

$(document).ready(function(){
    $("#Edit").click(function(){
    $.getJSON("fetchvalues.php?UpdateRecordID=" + $.cookie('UpdateRecordID'),
    function(data){
    // Data retrieved in concatenated form. So we will break it and store values in array.
    var concatenatedvalues = new Array();
    concatenatedValues = data;
    alert(concatenatedValues);
    });
});
});

The data is successfully returned, but I do not understand how to get each element of the array through javascript. What changes are needed in the above code?

+3
source share
1 answer

Update I'm just re-reading your question, and it looks like you intentionally combined values. As you use json_encode, it would be much better to send the values ​​as an array and just access it in JavaScript.

echo json_encode(array($PostedDate, $Places, $Company, $Designation, $ProjectDetails, $DesiredCandidate, $HRName, $HRContact, $Email));

Then in JavaScript they will be available as follows:

alert(data[1]); // Would alert the value of $Places
+3

Source: https://habr.com/ru/post/1726602/


All Articles