Answer the question
In order to leave comments, you need to log in
How to pass array from php to javascript?
Hello!
How to pass php array to javascript?
In the php code we have:
$kolz5 = mysql_query("SELECT * FROM numbers") or die ("error: ".mysql_errno());
$content = array();
while (($row=mysql_fetch_assoc($kolz5)) !== false) { # запись запроса в массив
$content[] = $row ; }
alert(data);
Answer the question
In order to leave comments, you need to log in
PHP:
$content = array('абвгд', 'abcdefg', '12345');
echo json_encode($content);
<html>
<head>
<meta charset="utf-8">
</head>
...
</html>
Collation: utf8_general_ci
// php 5.4+
json_encode($content, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
<script type="text/javascript">
var ar = <?php echo json_encode($ar) ?>;
</script>
<script type="text/javascript">
var fruits = <?php echo '["' . implode('", "', $fruits) . '"]' ?>;
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question