Checkbox Array, checking the checkbox based on the value
I am using a combination of PHP and jQuery on this multipage form. I am storing the variables in the PHP Session, so if the user decides to go back a page, the values are filled in. I can get the Select element and text elements to cooperate, but my checkbox array is causing me difficulty.
PHP code to create checkbox array originally on the page:
<?
$sql2 = 'SELECT s.index, s.date, s.meetName, s.location
FROM schedule s
ORDER BY s.index';
$queryresults2 = mysqli_query($mysqli, $sql2);
if (!$queryresults2 ) {
die('Invalid query: ' . mysqli_error());
}
while ($row2 = mysqli_fetch_assoc($queryresults2)) {
echo '<tr>';
echo '<td><INPUT TYPE="CHECKBOX" class="checklist" name="check_list[]" value="' . $row2["index"] . '"/></td>';
echo '<td>' . $row2["date"] . '</td>';
echo '<td>' . $row2["meetName"] . '</td>';
echo '<td>' . $row2["location"] . '</td>';
echo '</tr>';
}
?>
Now if the session variable exists, I want to then check the boxes based on the value from the session variable:
<?
if (isset($_SESSION["name"])) {
echo '<script>';
echo '$(document).ready(function() {';
echo '$(\'#name\').val(' . $_SESSION["name"] .');';
echo '$(\'#preferredEmail\').val("' . $_SESSION["email"] .'");';
echo '$(\'#preferredPhone\').val("' . $_SESSION["phone"] .'");';
$meetsSelected = $_SESSION['meetsSelected'];
foreach ($meetsSelected as $meetsToSelect) {
//CODE HERE TO CHECK APPROPRIATE CHECKBOXES
}
echo '});';
echo '</script>';
}
?>
I am getting stuck on the code within the foreach php statement above. I have tried 10 or so different ideas, but I can't seem to find a way to identify the checkboxes based on value and then checked them if they are included in the php variable of $meetsToSelect.
If there is a better way of doing this (either fully php or jquery), please let me know. Thank You.