Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created January 23, 2012 18:25
Show Gist options
  • Save ynonp/1664664 to your computer and use it in GitHub Desktop.
Save ynonp/1664664 to your computer and use it in GitHub Desktop.
PHP JSON Example
<?php
header('Content-Type: application/json' );
$a = array(10, 20, 30);
print json_encode($a);
<!DOCTYPE html>
<html>
<head>
<title>PHP JSON Example</title>
</head>
<body>
<div>
And the winner is ...
<span id="result"></span>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
var div_el = document.querySelector('#result');
$.get('get_numbers.php', function(numbers) {
console.dir(numbers);
// put the second into the div
div_el.innerHTML = numbers[1];
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment