Home > Mobile >  utilize PHP array as HTML selection
utilize PHP array as HTML selection

Time:09-24

What I got.

I got an PHP which performs a user LDAP query and stores the result in an array. Within the PHP I JSON_encode($myArray) the array and pass this to an JavaScript file with print_r($myArray). In JavaScript I fill a HTML selection with the myArray as source.

The PHP results looks like, which is fine:

["Mickey Mouse","Donald Duck","Minnie Mouse"]

Whats the problem?

Usually I would fill a source based selection like this, which works with JSON files but not in this case:

var fillUserSelection

for (var key in myArray) {
   fillUserSelection  = "<option>"   myArray[key]   "</option>"
}
document.getElementbyId("").innerHTML = fillUserSelection

I expected Mickey Mouse, Donald Duck and Minnie Mouse as options. Instead I receive each char as options. Like [,",M,i,c,k,e,y.. etc.

What I want.

I want only Mickey Mouse, Donald Duck and Minnie Mouse as options. What do I miss?

CodePudding user response:

Try something like this.

var myArray = JSON.parse(<?php echo json_encode($phpArray) ?>);

Now your array will work.

CodePudding user response:

    <?php $myarray = array("Mickey Mouse","Donald Duck","Minnie Mouse"); ?>
    
    <?php json_encode($myarray); ?>
    
    
      <?php for ($i = 0; $i < count($myarray); $i  ) {?>
      
      <?php echo "$myarray[$i] <br/>"; ?>
      
      <?php } ?>
  • Related