Note that there are some explanatory texts on larger screens.

plurals
  1. POWorking with checkbox in PHP and JS
    primarykey
    data
    text
    <p>I am creating a form with both radio buttons and checkboxes. I want to validate the form with both javascript onsubmit, and php. For php to work, I have to write the checkboxes and radiobuttons with [] in the name (e.g. ) I searched the other topics and saw that I can put a default checked value for radiobuttons to avoid the probably of people not picking a value. That's fine. However, I also need to make sure people check no more than 3 check boxes. This cannot be done with </p> <pre><code>var count = 0; for (x=0; x&lt;document.fic_rec.rec_genre.length; x++){ if (document.fic_rec.rec_genres[x].checked){ count++; } } if (count==0){ rec_error += "at least 1 genre; "; } else if (count&gt;3){ rec_error += "no more than 3 genres; "; } </code></pre> <p>I get an error saying "Cannot read property 'length' of undefined My HTML code looks like this:</p> <pre><code>&lt;ul class="sub_ul"&gt; &lt;li class="sub"&gt; &lt;input type="checkbox" name="rec_genres[]" id="action"value="action" /&gt; &lt;label for="action"&gt;Action/Adventure&lt;/label&gt;&lt;/span&gt;&lt;li class="sub"&gt;&lt;u&gt;Alternative reality&lt;/u&gt;&lt;/li&gt; &lt;li class="sub_sub"&gt;&lt;input type="checkbox" name="rec_genres[]" id=au_history value="au_history" /&gt; &lt;label for="au_history"&gt;AU History&lt;/label&gt;&lt;/li&gt;&lt;li class="sub_sub"&gt;&lt;input type="checkbox" name="rec_genres[]" id=au_universe value="au_universe" /&gt; &lt;label for="au_universe"&gt;AU Universe&lt;/label&gt;&lt;/li&gt;&lt;li class="sub_sub"&gt;&lt;input type="checkbox" name="rec_genres[]" id=au_other value="au_other" /&gt; </code></pre> <p>etc etc</p> <p>Help! Thanks!!</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