Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP MultiCheckbox Processing Returns Null
    primarykey
    data
    text
    <p>I had a PHP processing script for an HTML form that had been working in the past. We had used the form extensively and everything was getting into the DB just fine. Now I'm using it again (on the same page, it had just been inactive till now) and I'm running into some trouble with my checkboxes processing.</p> <p>I've been toying with it to try and see where I'm messing up and I've gathered that I must be mistakenly processing the checkboxes in PHP. This is what I have currently:</p> <p>HTML: </p> <pre><code>&lt;label for="race" style="padding-right: 15px;"&gt;Race:&lt;/label&gt; White &lt;input type="checkbox" name="raceWhite" value="White" /&gt; Black &lt;input type="checkbox" name="raceBlack" value="Black" /&gt; Asian/Pacific Islander &lt;input type="checkbox" name="raceAsian" value="Asian/Pacific Islander" /&gt; Hispanic/Latino &lt;input type="checkbox" name="raceHispanic" value="Hispanic/Latino" /&gt; Other &lt;input type="checkbox" id="race" name="raceOther" value="Other" /&gt; </code></pre> <p>And here's my PHP:</p> <pre><code>if (isset($_POST['raceWhite'])) $white = true; else echo "wtf is going on"; if (isset($_POST['raceBlack'])) $black = true; if (isset($_POST['raceAsian'])) $asian = true; if (isset($_POST['raceHispanic'])) $hispanic = true; if (isset($_POST['raceOther'])) $otherRace = true; </code></pre> <p>I'm concerned because I'm echoing out "wtf is going on" and if I am understanding correctly, that is because the checkbox for raceWhite is not checked when I am indeed checking it as I test this. Also, with this method, my DB reflects these as being NULL -- i.e. the checkboxes were not checked.</p> <p>However, with my old code, the DB flips all the values to 1 which is represents all the checkboxes being checked. My old code went like this:</p> <p>HTML:</p> <pre><code>&lt;label for="race" style="padding-right: 15px;"&gt;Race:&lt;/label&gt; White &lt;input type="checkbox" name="race[]" value="White" /&gt; Black &lt;input type="checkbox" name="race[]" value="Black" /&gt; Asian/Pacific Islander &lt;input type="checkbox" name="race[]" value="Asian/Pacific Islander" /&gt; Hispanic/Latino &lt;input type="checkbox" name="race[]" value="Hispanic/Latino" /&gt; Other &lt;input type="checkbox" id="race" name="race[]" value="Other" /&gt; </code></pre> <p>PHP:</p> <pre><code>$raceList = $_POST["race"]; $n = count($raceList); for($i = 0; $i &lt; $n; $i++){ switch($raceList[$i]){ case "White": $white = 1; case "Black": $black = 1; case "Asian/Pacific Islander": $asian = 1; case "Hispanic/Latino": $hispanic = 1; case "Other": $otherRace = 1; } } </code></pre> <p>Any help would be greatly appreciated. I'm sure I'm just missing something simple. This is my first time asking a question here so if I left out anything I should include in the future, please let me know.</p>
    singulars
    1. This table or related slice is empty.
    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