I have an object in PHP that I'm passing to Javascript in the same page. It seems to be passing through fine as a console.log
statement returns the expected result but when I try to turn it into a Javascript variable the "id" is replaced with a 1, no quotation marks, rather than the expected result. If I wrap the PHP statement in the Javascript in square brackets the "id" does not change, but then it's formatted oddly. What I'm wondering is why this is occurring and if there's something I should do differently.
ETA: Since there was some confusion: The input is "Data," below. That is generated from the results of a SQL query by way of an array_push statement. I have included the code for each row of the SQL query and the array_push statement below, but there does not seem to be a problem with the PHP side.
My expected output is simply the data to be transferred to a JSON object on the javascript side with the appropriate variable, using the code mentioned under "Javascript." Instead I am getting something like this as console.log
output:
{
DNB_number: "0"
color: "darkred"
id: 1
name: "Anonymous"
role: "Author"
size: null
type: "person"
witness_work_id: "1467"
witness_work_role_id: "1659"
}
That 1 under id is what is the issue.
There is nothing happening on the javascript side before the attempt to convert the result to a javascript variable (what you see under javascript below), so there's not much else I can tell you there. The conversion on the PHP side happens, I start my javascript, then immediately attempt to invoke it on the javascript side.
PHP code:
$person_color = "darkred";
foreach ($person_data as $row) {
$person_id = $row["person_id"];
$type = "person";
$witness_work_role_id = $row["Witness_work_role_id"];
$witness_work_id = $row["Witness_work_id"];
$person = $row["Person_name"];
$DNB = $row["Oxford_DNB_number"];
$role = $row["Role"];
$color = $person_color;
array_push($GLOBALS['person_array'], array(
'id'=>$person_id,
'witness_work_role_id'=>$witness_work_role_id,
'witness_work_id'=>$witness_work_id,
'type'=>$type,
'name'=>$person,
'DNB_number'=>$DNB,
'role'=>$role,
'color'=>$color,
'size'=> NULL
));
}
$jperson = json_encode($person_array);
Javascript code:
person_array = <?=$jperson; ?>;
Data:
[
{
"id": "1",
"witness_work_role_id": "1660",
"witness_work_id": "1468",
"type": "person",
"name": "Anonymous",
"DNB_number": "0",
"role": "Author",
"color": "darkred",
"size": null
},
{
"id": "152",
"witness_work_role_id": "1573",
"witness_work_id": "1384",
"type": "person",
"name": "Gilbert Banester",
"DNB_number": "0",
"role": "Author",
"color": "darkred",
"size": null
},
{
"id": "153",
"witness_work_role_id": "1574",
"witness_work_id": "1385",
"type": "person",
"name": "Elizabeth Plantagenet (Elizabeth of York)",
"DNB_number": "8635",
"role": "Author",
"color": "darkred",
"size": null
},
{
"id": "3",
"witness_work_role_id": "1642",
"witness_work_id": "1451",
"type": "person",
"name": "Geoffrey Chaucer",
"DNB_number": "5191",
"role": "Author",
"color": "darkred",
"size": null
},
{
"id": "7",
"witness_work_role_id": "1643",
"witness_work_id": "975",
"type": "person",
"name": "Benedict Burgh",
"DNB_number": "3390",
"role": "Author",
"color": "darkred",
"size": null
},
{
"id": "5",
"witness_work_role_id": "1659",
"witness_work_id": "1467",
"type": "person",
"name": "Anonymous",
"DNB_number": "0",
"role": "Author",
"color": "darkred",
"size": null
}
]