Insert multiple HTML form checkbox values in single database column - php

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. :)

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);

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

Related

how to input and display multi lines in php/html form?

So i am new to using Linux VPS. I want to install softether on my VPS to protect myself when i play online multiplayer games. My VPS does not have much DDOS protection (because it is cheap)so i learned a technique called IP tables. There seems to be a lot of bad ip addresses in many databases that I found online. So i started to block IPs, but found that it takes a long time. I wanted to write an IP table generator to make my life easier. I wanted to use a subnet calculator on this form but i did not know how to code it, so i use a online site to get the subnet ip addresses of an ip i going to block.I thought it might be easier to just block all corporate and commercial asn ip addresses. but again i don't know where to start. That would be better for me to block ALL corporate/commercial IPs when i play my online games and i can just whitelist the IPs from my xbox live servers. please share your opinion(s). i want to have a safe time playing online without attackers flooding my internet.
i want to add multiple ip address inside my generator. but i cannot remember what shall I google to be able to learn how to do it. ALSO my php code is not working.
MY AIM to add multiple ip address and config them to rules, filters and chain rules. AND display them at the bottom clean (with line breaks) .
<html><head><style>.error {color: #FF0000;}</style></head><body>
<?php
$addressErr = $rulesErr = $filtersErr = $cRULEErr = "";
$address = $rules = $filters = $cRULE = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
(empty($_POST["address"])) {
$nameErr = "ip address is required";
} else {
$name = test_input($_POST["address"]);
if (!preg_match("/^[0-9 ]*$/",$address)) {
$addressErr = "Only numbers and white space allowed";
}
}
if (empty($_POST["rules"])) {
$rules = "Rules is required";
} else {
$rules = test_input($_POST["rules"]);
}
}
if (empty($_POST["filters"])) {
$filters = "Filters is required";
} else {
$filters = test_input($_POST["filters"]);
}
}
if(isset($_POST['generate'])){
if(!empty($_POST['cRULE'])){
foreach($_POST['cRULE'] as $selected){
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$data= $address + $rules + $filters + $cRULE + $selected."</br>";
echo $data "</br>"
?>
<p>
<form class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>ip Table Generator v1</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="address">IP</label>
<div class="col-md-4">
<input id="address" name="address" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="rules">RULES</label>
<div class="col-md-4">
<label class="radio-inline" for="rules-0">
<input type="radio" name="rules" id="rules-0" value="ACCEPT" checked="checked">
accept
</label>
<label class="radio-inline" for="rules-1">
<input type="radio" name="rules" id="rules-1" value="DROP">
drop
</label>
<label class="radio-inline" for="rules-2">
<input type="radio" name="rules" id="rules-2" value="RETURN">
return
</label>
</div>
</div>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="filters">FILTERS</label>
<div class="col-md-4">
<label class="radio-inline" for="filters-0">
<input type="radio" name="filters" id="filters-0" value="INPUT" checked="checked">
input
</label>
<label class="radio-inline" for="filters-1">
<input type="radio" name="filters" id="filters-1" value="FORWARD">
forward
</label>
<label class="radio-inline" for="filters-2">
<input type="radio" name="filters" id="filters-2" value="OUTPUT">
output
</label>
</div>
</div>
<!-- Multiple Checkboxes (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="cRULE">Chain Rules</label>
<div class="col-md-4">
<label class="checkbox-inline" for="cRULE-0">
<input type="checkbox" name="cRULE" id="cRULE-0" value="APPEND">
-A (Append)
</label>
<label class="checkbox-inline" for="cRULE-1">
<input type="checkbox" name="cRULE" id="cRULE-1" value="INTERFACE">
-i (interface)
</label>
<label class="checkbox-inline" for="cRULE-2">
<input type="checkbox" name="cRULE" id="cRULE-2" value="PROTOCOL">
-p (protocol)
</label>
<label class="checkbox-inline" for="cRULE-3">
<input type="checkbox" name="cRULE" id="cRULE-3" value="SOURCE">
-s (source)
</label>
<label class="checkbox-inline" for="cRULE-4">
<input type="checkbox" name="cRULE" id="cRULE-4" value="dPORT">
–dport
</label>
<label class="checkbox-inline" for="cRULE-5">
<input type="checkbox" name="cRULE" id="cRULE-5" value="TARGET">
-j (target)
</label>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="GEN"></label></br>
<div class="col-md-4">
<button id="GEN" name="GEN" class="btn btn-primary">GENERATE</button>
</div>
</div>
</fieldset>
</form>

How to generate questionnaire form with a MySQL table?

I have a "questionnaire" table which contains question_id, question, and variable_scored (with rows like variable1, variable2, etc.) fields.
What I'm trying to do is generating an HTML questionnaire form based on the content of the table like so
"Question 1 here"
4 radio buttons inputs value for variable1
"Question 2 here"
4 radio buttons inputs value for variable2
Here's my code of current still static form
<div class="form-group">
<label for="input2" class="col-sm-12">Question Here</label>
<div class="col-sm-12">
<div class="radio">
<label><input type="radio" name="variable1" value='4'>4</label>
</div>
<div class="radio">
<label><input type="radio" name="variable1" value="3">3</label>
</div>
<div class="radio">
<label><input type="radio" name="variable1" value="2">2</label>
</div>
<div class="radio">
<label><input type="radio" name="variable1" value="1">1</label>
</div>
I thing you can fetch all details from controller and pass this array into view file.
And view file you can just assign this value like.
<label for="input2" class="col-sm-12"><?php echo $data['question'] ?></label>
<div class="radio">
<label><input type="radio" name="<?php echo $data['variable1'] ?>" value='4'>4</label>
</div>
You can try this one may it works for you.
I provide you also link for you.
link

on selected value of radio button get html form

I have two registration HTML form for Student and College .
Initially, I have two radio buttons of Student and College. If I select Student I'll get student registration form or if I select College I'll get college registration form also I want to insert that value of radio button to the database. I'm unable to understand how should I start. I'm using PHP and MySQLi technology. Please help me with this?
<div class="col-lg-6">
<h5>Whether College/Student?</h5>
<div class="form-group form-check-inline">
<label class="custom-control custom-radio">
<input id="radio" name="radio" value="0" type="radio" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">College</span>
</label>
</div>
<div class="form-group form-check-inline">
<label class="custom-control custom-radio">
<input id="radio" name="radio" value="1" type="radio" class="custom-control-input">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Student</span>
</label>
</div>
</div>
First of all you need a form. You can read this article to understand how to do this and how get your form values with form tutorial
After you'll need to put those values in a database. To do that i recommand you PDO
But you probably should practice withsome tutorial before did you try to do this one?

Validating selected checkboxes (array) with 'in'

I have the following checkboxes in my form:
<div class="form-group">
<label for="likes_turtles" class="col-md-3 control-label">Likes Turtles</label>
<div class="col-md-6">
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="Yes"> Yes
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="No"> No
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="Maybe so"> Maybe so
</label>
</div>
</div>
</div>
As you can see, they are an array of items because I want to allow selection of multiple items.
I am trying make it so that the checked items must be valid.
Here is my validation rule:
[likes_turtles] => required|in:Yes,No,Maybe so
The problem is, I'm getting the following error when I check any or all of these items:
The selected likes turtles is invalid.
How do I make laravel simply ensure any items in the request array likes_turtles belong to the specified list e.g. Yes,No,Maybe so
According to the documentation on validating arrays, you could do it like this:
[
"likes_turtles" => "required",
"likes_turtles.*" => "in:Yes,No,Maybe so"
]

fetch data from database and check the checkbox

i have a problem here to check the checkbox from database.
because there is textbox inside my checkbox.
My checkbox looks like this : https://jsfiddle.net/qqeh07e5/
First of all. It is okay to check the checkbox if there no textbox inside checkbox, because i saved the value of checkbox. But if there is a textbox inside the checkbox, i don't save checkbox's value, but save textbox's value. So my problem how to make the value from database know that its belong textbox A, not textbox B.
Here is i've done so far.
<?php
$temp_detailEvent = $_SESSION['detail_event'];
$q = sqlsrv_query($conn,"SELECT * FROM EventDetail WHERE id_eventDetail='$temp_detailEvent'");
while($row = sqlsrv_fetch_array($q))
{
$tuj = explode(",",$row['tujuanTraining']);
<div class="col-lg-4">
<div class="form-group">
<label>Latar Belakang</label>
<div class="checkbox">
<label>';
?>
<input type="checkbox" name="tujuan[]" id="cekbox_tujuan1" <?php if(in_array("Pencapaian Kompetensi Karyawan",$tuj)) { ?> checked="checked" <?php } ?> value="Pencapaian Kompetensi Karyawan">Pencapaian Kompetensi Karyawan
<?php
echo '</label>
</div>
<div class="checkbox form-inline">
<label>
<input type="checkbox" id="cekbox_latarbelakang2" value="Mengacu pada Analisa GAP pada FPT Nomor">Mengacu Pada Analisa GAP pada FPT Nomor
</label>
<input type="text" id="textbox_latarbelakang1" name="latarbelakang[]" class="form-control input-sm" placeholder="">
</div>
<div class="checkbox form-inline">
<label>
<input type="checkbox" id="cekbox_latarbelakang3" value="">
Lain - lain
</label>
<input type="text" id="textbox_latarbelakang2" name="latarbelakang[]" class="form-control input-sm" placeholder="Lain - lain">
</div>
</div>
</div>';
}
i am just check if there is a value same in database with in_array
Hope someone can help me, thank you very much. Hv a nice day and CHEERSS!!

Categories