vendredi 18 mai 2018

Can I keep looped checkboxes in php checked after submission of the form?

I have written a piece of code that generates a table with values, with the last column having a checkbox. Now I want to make sure that checkboxes that were checked before pressing the submit button are still checked after the button has been pressed, so people can still see what values were used to make the calculations and don't have to re-check a whole bunch of checkboxes if they want to make calculations again but with extra or fewer values.

This is my code for the table: note that this is all written in between php tags. The list that needs to be in the table is a dynamic one, hence the use of php to loop the checkboxes rather than making them in html.

<?php echo "<form class='form_list' action='' method='POST' enctype='multipart/form-data'>";
    echo "<table class='calc'>";
    echo "<tr><th>Merker</th><th>Type</th><th>% chimerism</th><th>Max stutter<br> ratio</th><th>Include?</th></tr>";
        for ($j=0; $j < $typecount; $j++) { // the colummn for max ratio contains a ternary operator to give the values the acceptable or too high colors + a second ternary to determine whether a checkbox should be checked based on stutter ratio values.
            echo "<tr>";
                echo "<td style='font-weight: bold;'>" . $_SESSION['all_merkers'][$j] . "</td>";
                echo "<td>" . $_SESSION['types'][$j] . "</td>";
                echo "<td style='text-align: center;'>" . $_SESSION['all_chim'][$j] . " %</td>";
                echo "<td style='text-align: center;'>" . ($_SESSION['max_ratio'][$j] > 1 ? '<span class="stutterbad">' : '<span class="stuttergood">') . $_SESSION['max_ratio'][$j] . "</span></td>";
                //if (!empty($_POST['merker'])) {
                    // Code to keep checkboxes checked.
                //}
                //else {
                echo "<td style='text-align: center;'>" . ($_SESSION['max_ratio'][$j] > 1 ?
                                                        "<input type='checkbox' name='merker[]' value=" . $_SESSION['all_chim'][$j] . ">" :
                                                        "<input type='checkbox' name='merker[]' value=" . $_SESSION['all_chim'][$j] . " checked>") . "</td>";
                //}
                echo "</tr>";
        }
    echo "</table>";
    echo "<h3>(*Type III merkers are not displayed in this table.)</h3>";
        echo"<p><input type='submit' class='extbutton' name='calc_chim' value='Calculate average with selected merkers.'><br>";
    echo "</form>"; ?>

I have seen many solutions concerning this topic but they all assume that the inputs are made in html while my checkboxes are generated using a loop. When trying to use an if statement after calling the value of the checkbox, I get an error stating that the if condition is unexpected.

I'd rather not use javascript, since I'm just a student and this project is my traineeship to finalize my education, I don't have a lot of time to learn javascript/jsquery, unless I have no choice.

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire