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>
5
  • 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 Commented Sep 7, 2017 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. Commented Sep 7, 2017 at 19:05
  • Never store values as a comma separated string in the DB, that is always a bad idea, use as many columns as needed. Commented Sep 7, 2017 at 19:15
  • Hmmm, issue is, i want to have like 20 options to choose from.. but the other point.. you're saying just <input type="checkbox" name="one"> ... then.. $box1 = $_POST['one']; Commented Sep 7, 2017 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? Commented Sep 7, 2017 at 19:24

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

12 Comments

Ah yeah, thing is, the only way i was able to create the Dropdown Checkbox, was through CSS and JS
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.
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?
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..
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: codepen.io/elmahdim/pen/hlmri
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.