I'm trying to generate a list of names with checkboxes using AJAX. Unfortunately, using the code below, I only get one name back instead of the four that are in my database.
Here is the code I'm using which sits in a separate php file called by AJAX:
$query = "SELECT id, first_name, last_name, email FROM users WHERE location_id = '{$location_id}' ";
$result = mysqli_query($connection, $query);
if (!$result) {
die("Database query failed: " . mysqli_error($connection));
}
while ($row = mysqli_fetch_array($result)) {
$name = $row['first_name'] . " " . $row['last_name'];
$attendees = '<label><input type="checkbox" name="emp_name[]" id="emp_name[]" value="' . $name . '"> ' . $name . '</label><span style="padding-right: 20px;"></span>';
}
echo json_encode(array('attendees'=>$attendees));
Before I attempted this AJAX call I had the same query in my main document with echo instead of $attendees and it worked flawlessly. It returned all four names with checkboxes next to them. Here's a snippet from that code:
while ($row = mysqli_fetch_array($result)) {
$name = $row['first_name'] . " " . $row['last_name'];
echo '<label><input type="checkbox" name="emp_name[]" id="emp_name[]" value="' . $name . '"> ' . $name . '</label><span style="padding-right: 20px;"></span>';
}
I'm very new to AJAX and so I'm not sure where the problem is. Please help!