Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass PHP array into Javascript array [duplicate]

Tags:

javascript

php

Referring to the title. I have an array which I coded like this:-

$query = "SELECT * FROM server";
$result = mysql_query($query);
$dServer = array();

while($row = mysql_fetch_assoc($result)) {
    $dServer[] = $row['model'];
}    

Now, how do I pass the $dServer array into a Javascript array?

For example, this array:

var a = new Array();
like image 575
Hafiz Abdullah Avatar asked Apr 19 '12 01:04

Hafiz Abdullah


1 Answers

$query = "SELECT * FROM server";
$result = mysql_query($query);
$dServer = array();

while($row = mysql_fetch_assoc($result)){
    $dServer[] = $row['model'];
}    

?>
<script type="text/javascript">
    var a = <?php echo json_encode($dServer); ?>;
</script>
like image 135
Paul Avatar answered Sep 22 '22 19:09

Paul