mardi 21 février 2017

change checkbox checked via session variable

I have a form with checkboxes which are used to select data from mysql. I also would like to have the checkboxes to stay checked if the user has done so. In addition to that, those checkboxes are generated from a mysql select. Each user has different checkboxes based on their permission levels etc.

So I got this code working:

<form method="post" id="someselect">
<?php
    function checked(){if (in_array("somestring", $_SESSION['specificvalue'])) { return "checked"; };} 

    $i = 0;
    while($row = $result->fetch_assoc())
        {
        echo "<input id='array' type='checkbox' ".checked()." name='array[]'  value='". $row['specificvalue'] . "'>" .$row['longvalue']."<br>";
        };

    mysqli_close($con);

    //$_SESSION['specificvalue']=implode("','",$specificvalue);
    $_SESSION['specificvalue']=$specificvalue;
?>
<input type="submit" value="Submit">
</p>
</form>

It works, generates user specific checkboxes and the function checked() also works with the exeption that instead in_array("somestring", $_SESSION['subunit']) , i would like to use in_array($row['subunit'], $_SESSION['subunit']), which simply does not work. I am to noob to understand why or come up with a better solution. The point of this would be to generate checkbox list and already check the boxes that have been stored in session variable before.

At the moment, with this working code. If i have the list of checkboxes and I check the box with specificvalue "somestring", all the checkboxes will be checked when the checkbox form is generated.

I think I would be able to use jquery for that but google gives me different kinds of solutions which cannot be used in mine. I also cannot manually write the checkboxes because there are ca 70 of them all together and they vary in time. I would like to get them autopopulated when the mysql database is updated.




Aucun commentaire:

Enregistrer un commentaire