1

beginner here. i have a nice mysqli user input going to where people fill out a form and it gets entered into my database.

For example im using

$category = $_POST['category'];

and

<input type="text" name="category" required placeholder="categories">

and its working great!

So.. i have a record which is a drop down of checkboxes, so the user can choose multiple answers. looks like this

<form>
  <div class="multiselect">
    <div class="selectBox" onclick="showCheckboxes()">
       <select>
          <option>Select an option</option>
         </select>
         <div class="overSelect"></div>
        </div>
        <div id="checkboxes">
           <label for="one"><input type="checkbox" id="one" />First checkbox</label>
           <label for="two"><input type="checkbox" id="two" />Second checkbox</label>
           <label for="three"><input type="checkbox" id="three" />Third checkbox</label>
         </div>
        </div>
       </form>
  1. How do i use

    $choices = $_POST['choices'];

to connect to the multiple choices the user chooses.

2.and how do i name the form above, so i can INSERT INTO (choices) VALUES ($choices)

  1. i suppose it should be inputted into my database separated by a comma, (choice1, choice2, choice3)

ADDITIONAL INFO I HAD TO USE JS AND CSS TO CREATE THE DROPDOWN CHECKBOXES.

              <style>
                      .multiselect {
                        width: 200px;
                      }
                      .selectBox {
                        position: relative;
                      }  
                      .selectBox select {
                        width: 100%;
                        font-weight: bold;
                      }
                      .overSelect {
                        position: absolute;
                        left: 0; right: 0; top: 0; bottom: 0;
                      }
                      #checkboxes {
                        display: none;
                        border: 1px #dadada solid;
                      }
                      #checkboxes label {
                        display: block;
                      }
                      #checkboxes label:hover {
                        background-color: #1e90ff;
                      }

                      </style>
                  Category:
                  <form>
                    <div class="multiselect">
                      <div class="selectBox" onclick="showCheckboxes()">
                        <select>
                          <option>Select an option</option>
                        </select>
                        <div class="overSelect"></div>
                      </div>
                      <div id="checkboxes">
                        <label for="one"><input type="checkbox" id="one" />First checkbox</label>
                        <label for="two"><input type="checkbox" id="two" />Second checkbox</label>
                        <label for="three"><input type="checkbox" id="three" />Third checkbox</label>
                      </div>
                    </div>
                  </form>

                  <script>
                    var expanded = false;
                    function showCheckboxes() {
                      var checkboxes = document.getElementById("checkboxes");
                      if (!expanded) {
                        checkboxes.style.display = "block";
                        expanded = true;
                      } else {
                        checkboxes.style.display = "none";
                        expanded = false;

                        }
                    }
                  </script>
  • 1st: give the checkboxes a name, the same way as you did for 'categories' (id is useless here) - then you can access them here `$_POST['firstcheckbox']`. The value will only be set if the checkbox is checked. [See here](https://stackoverflow.com/questions/11424037/does-input-type-checkbox-only-post-data-if-its-checked) – Jeff Sep 07 '17 at 19:01
  • 3rd: no, rather have 3 differend fields for that (if it's a checkbox, not a radio). Otherwise you will get in trouble when you wanna search for them. – Jeff Sep 07 '17 at 19:05
  • **Never** store values as a comma separated string in the DB, [that is always a bad idea](https://stackoverflow.com/a/3653574/49934), use as many columns as needed. – Alberto Martinez Sep 07 '17 at 19:15
  • Hmmm, issue is, i want to have like 20 options to choose from.. but the other point.. you're saying just ... then.. $box1 = $_POST['one']; – Charlesx54321 Sep 07 '17 at 19:15
  • Updated it. So, Do you think it would be smart to limit the user to only choosing 3 checkboxes per post... that way i will only need 3 spaces in my Databse... even though i have 30 options? – Charlesx54321 Sep 07 '17 at 19:24

1 Answers1

1

The attribute "id" is only used by CSS and javascript to refer to the specific element. What you want here, is give the elements a name. like so:

<div id="checkboxes">
     <label for="one"><input type="checkbox" name="one" id="one" />First checkbox</label>
     <label for="two"><input type="checkbox" name="two" id="two" />Second checkbox</label>
    <label for="three"><input type="checkbox" name="three" id="three" />Third checkbox</label>
</div>

You can then check in your $_POST for $_POST['one'] or $_POST['two'] or $_POST['three']. They should be either missing entirely (use isset()) or should be posted as the value "on"

if (isset($_POST['one']) && $POST['one'] === "on"){
   // do stuff for checkbox one
}
Erik Baars
  • 2,278
  • 1
  • 8
  • 14
  • Ah yeah, thing is, the only way i was able to create the Dropdown Checkbox, was through CSS and JS – Charlesx54321 Sep 07 '17 at 19:17
  • If you update your question with the way you create the checkboxes, i can probably tell you how to add the name property to it. – Erik Baars Sep 07 '17 at 19:19
  • Updated it. So, Do you think it would be smart to limit the user to only choosing 3 checkboxes per post... that way i will only need 3 spaces in my Databse... even though i have 30 options? – Charlesx54321 Sep 07 '17 at 19:23
  • And was there a much easier way to make those checkboxes drop down? even the other type of auto complete drop down that becomes tags would do the same thing.. – Charlesx54321 Sep 07 '17 at 19:25
  • The way your checkboxes are created does not stop you from adding a "name" property to them. As to answer your question: is there an easier way, try this: https://codepen.io/elmahdim/pen/hlmri – Erik Baars Sep 07 '17 at 19:26
  • haha that looks like the same amount of coding/work! but do you think my plan to have 3 spots yet 20 options good for what im trying to achieve? – Charlesx54321 Sep 07 '17 at 19:31
  • If youre doing it to save database space, i would not. I dont know what type of data youre trying to store, but you could implode the data into one record (1,2,5,6) or use a separate table to store a key value pair. – Erik Baars Sep 07 '17 at 19:41
  • basically, i want to store one word attributes.. So if i have Red blue purple orange (and 20 other colors) and want the user to choose only 3 choices... I can have the 3 selected, separated by comma on one record (attributes) or i can have 3 records At1, At2, At3 all of which you can select from 20 options.. (i will plan to export to excel and measure them eventually, but i can count them easily whether theyre separated by commas or in 3 separate records).... What do you think i should do..? – Charlesx54321 Sep 07 '17 at 19:51
  • If they only have 3 options, use 3 different columns in a database. That way you can easily search, filter, and later expand with an extra column should the need arise. – Erik Baars Sep 07 '17 at 20:10
  • No worries, thats what this site is for. Dont forget to accept my answer if it indeed helped :) – Erik Baars Sep 07 '17 at 20:18
  • Hey man, more issues... to test, i click the "First" Checkbox, so i expect my at0 record to say "first"... it shows up in my database in the right place, but it shows up as "on" ... – Charlesx54321 Sep 12 '17 at 18:56
  • Also, what it seems to do... When i click First check box, the word "on" appears on my at0 columns, when i click "Second" the word "on" inputs into my at1 column... etc – Charlesx54321 Sep 12 '17 at 19:03