Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring and updating checkbox array with PHP sessions
    primarykey
    data
    text
    <p>I have a signup form with a list of sports in checkbox form (pulled from a mysql table). On post, I create a session with variables pertaining to the form that can be validated. Everything works as planned except storing the checkboxes of the sports. I am already passing my post values through an array so it's like passing an array through an array and that's where I'm not having any luck. I just want the checked sports from post to show. Additionally, if that list is updated on a 2nd or 3rd validation I want that to update the array. Code is as follow, thanks in advance.</p> <p>Post code</p> <pre><code>if(isset($_POST['signup']) == 'Finish') { //start session to store variables for post-submit validation session_start(); //sanitizes input and prevents sql injection attacks foreach($_POST as $key =&gt; $value) { $data[$key] = filter($value); $_SESSION[$key] = $value; } $_SESSION['sport'] = array_unique(array_values(array_merge($_SESSION['sport'], $data['sport']))); </code></pre> <p>Form code</p> <pre><code> &lt;table class="signup_sportstable" border="0" cellpadding="0" cellspacing="4"&gt;'; $result = mysql_query("SELECT * FROM sports ORDER BY sport ASC"); $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { foreach ($_SESSION['sport'] as $sport_id) { $checked = 'checked'; } // make the variables easy to deal with extract($row); // open row if counter is zero if($i == 0) $content .= '&lt;tr&gt;'; // make sure we have a valid product if($sport != "" &amp;&amp; $sport != null) $content .= '&lt;td&gt;&lt;input type="checkbox" name="sport[]" value="'.$sport_id.'" id="sport[]" '.$checked.'&gt;'.$sport.'&lt;/td&gt;'; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { $content .= '&lt;/tr&gt;'; $i=0; } // end if } // end while // clean up table if($i &gt; 0) { for($j=$i; $j&lt;$max_columns;$j++) $content .= "&lt;td&gt;&amp;nbsp;&lt;/td&gt;"; $content .= '&lt;/tr&gt;'; } $content .= ' &lt;/table&gt; </code></pre> <p><strong>Update!!!</strong></p> <p>I forgot to mention initially that my code checks every box once the session is created, rather than just checking the boxes that were initially checked. I was informed this was because my checked condition based on my code was always true,so...</p> <p>Replaced:</p> <pre><code>foreach ($_SESSION['sport'] as $sport_id) { $checked = 'checked'; } </code></pre> <p>With:</p> <pre><code>if (in_array($sport_id, $_SESSION['sport'])) { $checked = 'checked'; } else { $checked = ''; </code></pre> <p>Now, it remembers two checkboxes but after every submit it checks the one immediately to the right of the one i checked the first time. If I hit submit again, its another spot to the right. }</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload