0

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
2
  • 1
    What's the output of the php? Certainly worth editing your question to include it. Commented Jul 12, 2016 at 18:30
  • Could you maybe give us the value of print_r($nominalCodes); or echo $nominalCodesData; from your PHP? Commented Jul 12, 2016 at 18:34

2 Answers 2

1

Try the following JavaScript instead:

//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];
    var option = document.createElement("option");
    option.value = obj["value"];
    option.text = obj["label"];
    selectList.appendChild(option);
}
Sign up to request clarification or add additional context in comments.

Comments

1

JSON indexes become properties in JavaScript, so, instead of obj[key] you must write obj.key. Next is the code (pointed by arrows ◄======):

<?php
//create array of items for nominal code box
$nominalCodes = array();
//$array = Statuses('Nominal Codes');
// SAMPLE DATA ▼
$nominalCodes[] = array('value' => "aaa", 'label' => "111" . ' (aaa)');
$nominalCodes[] = array('value' => "bbb", 'label' => "222" . ' (bbb)');
$nominalCodes[] = array('value' => "ccc", 'label' => "333" . ' (ccc)');
$nominalCodesData = json_encode($nominalCodes);
?>
<html>
  <head>
  </head>
  <body>
    <div id="my_div">
    </div>
    <script type="text/javascript">
//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";
    document.getElementById( "my_div" ).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.value;         //◄============================
            option.text = obj.label;          //◄============================
            selectList.appendChild(option);
      //}
    }
    </script>
  </body>
</html>

Copy-paste previous code in a file, save it as PHP and open it in your browser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.