Created
May 2, 2013 00:40
-
-
Save haykuro/5499436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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