1 Replies - 914 Views - Last Post: 01 February 2013 - 01:58 PM Rate Topic: -----

#1 stefanere2k9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-November 11

populating dropdown box with json data

Posted 01 February 2013 - 07:59 AM

i have got this code so far but it don't seem to be doing what i need, i have followed a few examples but this is what ive done at top of my head. hope someone can help. just need to populate the table now

<title>Untitled Document</title>
</head>

<script src="jquery.js"></script>
<script src="jquery.jCombo.js"></script>
<style type="text/css">
</style>

<?php
$directory = opendir("C:/xampp/htdocs/SE3S604CW3/xml");

$storeFilesIntoArray[] = "please select XML to load";

while (($fileName = readdir($directory)) !== false)
{
 $getLastFourDigitsOfFile = substr($fileName,-4); 
 
 if($getLastFourDigitsOfFile == ".xml")
 {
  #echo "filename is " . $fileName . "<br/>";
    $storeFilesIntoArray[] = $fileName;
 }
}
$json = json_encode($storeFilesIntoArray);
echo $json;
//close directory and print the contents of the array to check if its working.
closedir($directory);

?>

<body>

<script type="text/javascript">

$(document).ready(function(){
	$("#list1").jCombo($json, {} );

});

</script>

<select name="list1" id="list1"></select><br><br>

</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: populating dropdown box with json data

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 801
  • View blog
  • Posts: 1,700
  • Joined: 30-January 09

Re: populating dropdown box with json data

Posted 01 February 2013 - 01:58 PM

You're mixing languages. A PHP variable cannot be directly referenced by Javascript, since PHP is server-side and Javascript is client-side. What you can do though is wrap the reference to $json in PHP tags and then echo it out so that it becomes part of the served Javascript. Try changing line 36 to this:
    $("#list1").jCombo(<?php echo $json; ?>, {} );


...or, if you are using PHP5.4+, you can use short tags:
    $("#list1").jCombo(<?= $json ?>, {} );


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1