info php ->javascript possible?

Discussion in 'JavaScript' started by gilgalbiblewheel, Jul 8, 2009.

  1. #1
    I'm debating how to use a php array of 39 color names within a javascript code.
    
    <?php
    $COLORS = Array(
    "Red",
    "Blue",
    "Green",
    "Orange",
    "Pink",
    "Sienna",
    "DarkOliveGreen",
    "DarkGreen",
    "DarkSlateBlue",
    "Navy",
    "Indigo",
    "DarkSlateGray",
    "DarkRed",
    "DarkOrange",
    "Olive",
    "Teal",
    "SlateGray",
    "DimGray",
    "SandyBrown",
    "YellowGreen",
    "SeaGreen",
    "MediumTurquoise",
    "Purple",
    "Gray",
    "Magenta",
    "Yellow",
    "Lime",
    "Cyan",
    "DeepSkyBlue",
    "DarkOrchid",
    "Silver",
    "Wheat",
    "LemonChiffon",
    "PaleGreen",
    "PaleTurquoise",
    "LightBlue",
    "Plum",
    "Black",
    "White"
    );
    ?>
    
    PHP:

     
    gilgalbiblewheel, Jul 8, 2009 IP
  2. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <form action="">
    <select name="colors">
    [php]<?php
    foreach ($COLORS as $COLOR){
    echo "<option value='$COLOR'>$COLOR</option>";
    ?>[/php]
    </select>
    </form>
    Code (markup):
    you could do something like that and you might have to edit it a little
     
    anthonywebs, Jul 8, 2009 IP
  3. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #3
    a straight export without loops / iterations from php into js:

    <script type="text/javascript">
    var COLORS = ['<?=implode("','", $COLORS)?>']; // will print ... = ['Red','Blue',...];
    </script>
    PHP:
    also useful: consider json_encode / json_decode (look them up on php manual, very handy for transferring of data structures)
     
    dimitar christoff, Jul 12, 2009 IP