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. :)
Aucun commentaire:
Enregistrer un commentaire