I think I'm on the right track now, but not quiet there yet.
Here's my Javascript code now:
$(function() {
var letters = $("ul.letters li");
var current = $("ul.letters li.selected");
var d = '';
letters.click(function() {
d = $(this).attr("title");
current.removeClass("selected");
current = $(this);
$(this).addClass("selected");
$.post('includes/getPeople.php', {char: d}, function(data) {
alert(data); //outputs null
var markup = '<table class="resultsTable">';
markup += '<tr><td>Full Name</td><td>Occupation</td><td>Phone Number</td></tr>';
var count = 0;
while(data != null) {
if(count % 2 == 0) {
markup += '<tr class="even">';
markup += '<td>' + data.people[count].FirstName + ' ' + data.people[count].LastName + '</td>';
markup += '<td>' + data.people[count].OccupationName + '</td>';
markup += '<td>' + data.people[count].Phone1 +'</td>';
markup += '</tr>';
count++;
} else {
markup += '<tr class="odd">';
markup += '<td>' + data.people[count].FirstName + ' ' + data.people[count].LastName + '</td>';
markup += '<td>' + data.people[count].OccupationName + '</td>';
markup += '<td>' + data.people[count].Phone1 +'</td>';
markup += '</tr>';
count++;
}
}
markup += '</table>';
alert(markup); //still doesn't alert anything
$("#allPeople").append(markup);
}, "json");
});
});
And the PHP (moved to getPeople.php) code using the json_encode() function:
include_once "search.php";
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
if(isset($_POST['char'])) {
$char = $_POST['char'];
$result = findPeople($char);
$count = 0;
$json = '"people":[';
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$json .= json_encode($row);
print $json;
}
$json .= ']';
return $json;
}
This is what I get when I use echo $json from PHP:
But when I use return $json, I get nothing in the console, BUT the alert(markup) in Javascript gets triggered showing no results though.
Am I missing anything here?