Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I make radio buttons, select boxes, and checkboxes required fields in a form?
    primarykey
    data
    text
    <p>I've set up my code so that it would require all the fields within the form, but for some reason it's only applying to input types of text, email, and password. So my question is how can I get the radio buttons, select boxes, and checkbox to also be required fields within the form? Here's my code:</p> <pre><code>&lt;form action="" method="post"&gt; &lt;ul id="register"&gt; &lt;li&gt; &lt;input type="text" name="first_name" placeholder="First Name"&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="text" name="last_name" placeholder="Last Name"&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="email" name="email" placeholder="Email"&gt;&lt;br&gt;&lt;br&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="password" name="password" placeholder="Password"&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="radio" name="sex" value="male"&gt;Male &lt;input type="radio" name="sex" value="female"&gt;Female &lt;/li&gt; &lt;li&gt; Birthday: &lt;select name="month"&gt; &lt;option value="January"&gt;January&lt;/option&gt; //all the other month options &lt;/select&gt; &lt;select name="day"&gt; &lt;option value="1"&gt;1&lt;/option&gt; //all the other days of the month &lt;/select&gt; &lt;select name="year"&gt; &lt;option value="2013"&gt;2013&lt;/option&gt; //ton of year options here &lt;/select&gt;&lt;br&gt;&lt;br&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="checkbox" name="terms_of_service" value="termsofservice"&gt;Terms of Service&lt;br&gt;&lt;br&gt; &lt;/li&gt; &lt;li&gt; &lt;input type="submit" name="registrationform" value="Sign up"&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/form&gt; &lt;?php if (empty($_POST) === false) { $required_fields = array('first_name', 'last_name', 'email', 'password', 'sex', 'birthday', 'terms_of_service'); foreach ($_POST as $key=&gt;$value) { if (empty($value) &amp;&amp; in_array($key, $required_fields) === true) { $errors[] = 'You didn\'t fill in all of the categories.'; break 1; } } } print_r($errors); ?&gt; </code></pre>
    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.
 

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