skip to Main Content

I am making an HTML form using Bootstrap 4 that contains three multiple-choice questions, allowing the reader to choose more than one answer using checkboxes.

Here is an example of one question in the form.

<div class="container">
    <form action="/survey/insert.php" class="was-validated" method="post">
        <div class="form-group">
            <legend class="form-check-label" for="tmp-area"><b>Please select the nearest city, town, or area where you have detailed knowledge of roads, trails, and places on public land.</b></legend>

            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Black Canyon City, Bumblebee, Cortes Junction" />Black Canyon City, Bumblebee, Cortes Junction </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Bouse, Brenda, Parker" />Bouse, Brenda, Parker </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Kingman, Lake Havasu City" />Kingman, Lake Havasu City </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Littlefield, Masquite" />Littlefield, Masquite </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Arizona Strip, Parashant National Monument" />Arizona Strip, Parashant National Monument</label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Tuscon, Winkelman" />Tuscon, Winkelman </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Buckeye, Rainbow Valley" />Buckeye, Rainbow Valley </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Tonto National Forest " />Tonto National Forest </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Coconino National Forest" />Coconino National Forest </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Apache Sitgreaves National Forest" />Apache Sitgreaves National Forest </label>
            </div>
            <div class="form-check">
                <label class="form-check-label" for="tmp_area"> <input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area" value="Prescott National Forest" />Prescott National Forest </label>
            </div>
        </div>
    </form>
</div>

Here is an example of /survey/insert.php

<?php
        //Database connection
        $servername = "localhost";
        $username = "user";
        $password = "password";
        $dbname = "name";
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }

        // Taking all values from the form data(input)
        $tmp_area = $_REQUEST['tmp_area'];
        

        //Insert form data into table. Set table and columns.
        $sql = "INSERT INTO Survey1 (tmp_area) VALUES ('$tmp_area')";

        if ($conn->query($sql) === true) {
            echo "Your survey has been submitted";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
        ?>

The problem

When the user selects multiple checkboxes, only a single checkbox value is added to the database column.

I would prefer that ALL checkbox values, that are selected by the user, insert into the same database column.

All examples and tutorials I have found online require that each checkbox have its own column. Or do not go into much detail on how to accomplish this.

My knowledge of PHP is limited and any help would be greatly appreciated. 🙂

2

Answers


  1. Change tmp_area to tmp_area[]

     <div class="form-check">
       <label class="form-check-label" for="tmp_area"><input type="checkbox" class="form-check-input" id="tmp_area" name="tmp_area[]" value="Bouse, Brenda, Parker" />Bouse, Brenda, Parker </label>
     </div>
    

    During Insert

    // Taking all values from the form data(input)
    $tmp_area= implode(",",$_REQUEST['tmp_area']); // Store this to your database
    

    If you need your tmp_area , then use this to convert to array again

    $tmp_area[] = explode(",",$your_tmp_area);
    
    Login or Signup to reply.
  2. I will suggest a alternative way, If you can store real value in db.

    Like this:

    Change input type name to array type.

    <input type="checkbox" class=".." id=".." name="tmp_area[]" value=".." />
    

    Create array value before insert to your database.

    $tmp_area = ""; // create a variable to store all data
    
    $tmp_area = implode(',',$_REQUEST['tmp_area']); // convert array type to string
    

    When you retrieve data you can,

    $tmp_area[] = explode(",",$data_tmp_area_replace_here); // convert string data from database to array
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search