I have successfully retrieved a response from a PHP file into Javascript, however it is returned as one long string; when I need to put each separate element into a separate place in the HTML.
$.post("includes/skill_scout_1.php", {
selected_player: selected_transfer_player
}, function (data, status) {
$("#transfers-scout-1").html(data);
});
I am looking to do something like this
$.post("includes/skill_scout_1.php", {
selected_player: selected_transfer_player
}, function (data, status) {
var obj = JSON.parse(data);
$("#transfers-scout-1").html(obj.scout1);
$("#transfers-scout-2").html(obj.scout2);
$("#transfers-scout-3").html(obj.scout3);
}
However it returns no data and I get the following error in the web console SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.
Within the PHP file I have this to return the data to Javascript as JSON:
$myObj->scout1 = $scout1;
$myObj->scout2 = $scout2;
$myObj->scout3 = $scout3;
$myJSON = json_encode($myObj);
echo $myJSON;
Any help would be greatly appreciated.
When I try the below #transfer-scout-4 fills with the entire string (as expected), but #transfer-scout-1, #transfer-scout-2 and #transfer-scout-3 are blank.
$.post("includes/skill_scout_1.php", {
selected_player: selected_transfer_player
}, function (data, status) {
$("#transfers-scout-4").html(data);
var obj = JSON.parse(data);
$("#transfers-scout-1").html(obj.scout1);
$("#transfers-scout-2").html(obj.scout2);
$("#transfers-scout-3").html(obj.scout3);
});
Thanks