简体   繁体   中英

Get values from JSON encoded array string

My .php code in a file "fetchvalues.php" 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:

$(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 being returned successfully, but I am not following how get each array element through javascript. What modifications are needed in the above code?

Update I just reread your question and it looks like you intentionally concatenated the values. Since you are using json_encode it would be much better to send the values as an array, and simply access it in JavaScript.

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

Then in JavaScript they would be accessed like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM