I have this code that creates an array in PHP and then uses json_encode for the array
<?php
//create array of items for nominal code box
$nominalCodes = array();
$array = Statuses('Nominal Codes');
foreach($array["results"] as $ret) {
$nominalCodes[] = array('value' => $ret["name"], 'label' => $ret["display"].' ('.$ret["name"].')');
}
$nominalCodesData = json_encode($nominalCodes);
?>
i am then trying to create a select element with options from the PHP array.
I want to be able to use the data from the PHP array (value = value of select option / label = display of select option)
I have tried this code:
//Create array of options to be added
var array = <?php echo $nominalCodesData; ?>;
//Create and append select list
var selectList = document.createElement("select");
selectList.id = "mySelect";
Cell0.appendChild(selectList);
//Create and append the options
for(var i = 0; i < array.length; i++) {
var obj = array[i];
for(var key in obj) {
var option = document.createElement("option");
option.value = obj[key];
option.text = key;
selectList.appendChild(option);
}
}
However, this is returning options in the select element that are:
label
value
label
value
label
value
label
value
label
value
label
value
label
value
label
value
label
value
print_r($nominalCodes);orecho $nominalCodesData;from your PHP?