Skip to content

Instantly share code, notes, and snippets.

@haykuro
Created May 2, 2013 00:40
Show Gist options
  • Save haykuro/5499436 to your computer and use it in GitHub Desktop.
Save haykuro/5499436 to your computer and use it in GitHub Desktop.
<?php
// in response to: http://www.reddit.com/r/PHPhelp/comments/1d7jsr/retrieving_dynamically_generated_values_from_a/
# NOTE: I wouldn't recommend keeping all of this in 1 file. For simplicity sake though, I will.
if(isset($_POST['ajax']) && $_POST['ajax'] == 'db_data')
{
$test_mode = true;
/* this variable is just included so you can easily swap between a query
* and the provided array. (for easy testing)
*/
if($test_mode == false)
{
$query = 'SELECT * FROM table';
$data = array();
while($foo = mysql_fetch_array($query))
{
$data[] = $foo;
}
}
else
{
$data = array('test'=>1);
}
echo json_encode($data);
exit;
}
?>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<input type="button" value="Press Me!" onclick="ajaxAction(); return false;" />
<textarea id="theLandingZone"></textarea>
<script type="text/javascript">
function ajaxAction()
{
$.post('', {ajax:'db_data'}, function(data)
{
// parse the data
data = $.parseJSON(data);
// output to debug console (F12 in chrome, or the little gear)
console.log(data);
// output to the textarea.
$("#theLandingZone").val(data.test);
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment