I have created some Javascript that makes an AJAX POST request to the PHP file below, titled 'listgroupsall.php'. However, I am not getting the proper data back.
The AJAX request and the PHP script are supposed to return groups that a member of the site belong to and display some information about them on the page that can be clicked to go to that group's page.
Here is the PHP code:
<?php
//CONTAINS MYSQL SERVER CONNECTION INFO AND OTHER GOODIES
require_once $_SERVER['DOCUMENT_ROOT'] . '/filepath/config.php';
//VARIABLES PASSED IN FROM THE AJAX REQUEST
$uid = $_POST['uid'];
$tid = $_POST['tid'];
$p = $_POST['page'];
$filter = $_POST['fieldcontent'];
//FORM MYSQL QUERY
$gaq = "SELECT g.gid, g.groupname, g.dispname, g.avpath FROM groups AS g INNER JOIN users_groups AS ug ON g.gid = ug.gid AND ug.uid = " . mysql_real_escape_string($tid);
//ADD CONSTRAINT BASED ON UID IF IT IS NOT PASSED IN AS 0
if($uid!="0"){
$gaq .= " INNER JOIN users_groups AS ug ON g.gid = ug.gid AND ug.uid = " . mysql_real_escape_string($uid); }
//ADD CONSTRAINT BASED ON FILTER IF IT IS NOT AN EMPTY STRING
if($filter!=""){
$gaq .= " WHERE g.dispname LIKE '%" . mysql_real_escape_string($filter) . "%'"; }
//ORDER THE RESULTS BY ASCENDING g.dispname
$gaq .= " ORDER BY g.dispname ASC";
//GET AMOUNT OF PAGES REQUIRED TO DISPLAY ALL ITEMS RETURNED,
// SUCH THAT 10 ITEMS FIT ON 1 PAGE
$pnum = ceil(mysql_num_rows(mysql_query($gaq))/10);
//DISPLAY 10 RESULTS, STARTING ON 'PAGE' PASSED IN TO AJAX REQUEST
$gaq .= " LIMIT " . ($p*10)-10 . ",10";
//EXECUTE FULL MYSQL QUERY
$gaqr = mysql_query($gaq);
//APPEND A STRING $galisthtml WITH HTML OF THAT BLOCK
// WHICH CAN BE WRITTEN STRAIGHT TO THE PAGE
$galisthtml = "";
while($row = mysql_fetch_array($gaqr)){
$gid = $row['gid'];
$gname = $row['groupname'];
$gdname = $row['dispname'];
$gavpath = $row['avpath'];
$galisthtml .= "<a href='http://galacticsoftware.net/prop/group.php?groupname=" . $gname . "'><div class=gblock><div class=gblockavcont>";
$galisthtml .= "<img src='/prop/assets/img/avatars/group/" . $gavpath . "' Alt='Group Avatar' height=48px width=48px></div>";
$galisthtml .= "<div class=gblocktextcont><h1>" . $gdname . "</h1>&" . $gname . "</div></div></a>";
}
//RETURN THE PAGE NUMBERS TOTAL
//AND THE GROUPS LISTING HTML
//(IN THAT ORDER)
echo json_encode(array($pnum, $galisthtml));
?>
When I call the AJAX request, I am first giving it the values of
uid = 0
tid = 23
p = 1
fieldcontent = ""
When the AJAX request returns, it sends back
[1,""]
The integer is correct, saying that all groups returned will fit on 1 page (there are 4 of them total). However, it appears that the string concatenation inside the while loop never executes. If I define $galisthtml with a non-empty string it is successfully returned, so I know I am returning my data correctly.
The while loop I have is used successfully in another area of my page when it echoes strings instead of concatenating a string, and after running the same MYSQL query.
I have researched this issue and even seen others concatenate strings inside a while loop like this with no issues. I have no idea what the issue could be.