首页
PHP
mySqli php - 用户在 1 条记录中输入多项选择复选框
mySqli php - 用户在 1 条记录中输入多项选择复选框
[英] mySqli php - User input multiple choice checkboxes into 1 record
本文介绍了mySqli php - 用户在 1 条记录中输入多项选择复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里是初学者.我有一个不错的 mysqli 用户输入,用于人们填写表单并将其输入到我的数据库中.
例如我使用
$category = $_POST['category'];
和
而且效果很好!
所以..我有一个记录,它是复选框的下拉列表,因此用户可以选择多个答案.看起来像这样
<div id="复选框"><label for="one"><input type="checkbox" id="one"/>第一个复选框</label><label for="two"><input type="checkbox" id="two"/>第二个复选框</label><label for="three"><input type="checkbox" id="three"/>第三个复选框</label>
</表单>
我如何使用
$choices = $_POST['choices'];
连接到用户选择的多个选项.
2.我如何命名上面的表单,以便我可以插入(选择)值($选择)
我想它应该输入到我的数据库中,以逗号分隔,(choice1, choice2, choice3) 附加信息我不得不使用 JS 和 CSS 来创建下拉复选框.
<样式>.多选 {宽度:200px;}.selectBox {位置:相对;}.selectBox 选择 {宽度:100%;字体粗细:粗体;}.overSelect {位置:绝对;左:0;右:0;顶部:0;底部:0;}#复选框{显示:无;边框:1px #dadada 实心;}#checkboxes 标签{显示:块;}#checkboxes 标签:悬停{背景颜色:#1e90ff;}</风格>类别:<表格><div class="multiselect"><div class="selectBox" onclick="showCheckboxes()"><选择><option>选择一个选项</option></选择><div class="overSelect"></div>
<div id="复选框"><label for="one"><input type="checkbox" id="one"/>第一个复选框</label><label for="two"><input type="checkbox" id="two"/>第二个复选框</label><label for="three"><input type="checkbox" id="three"/>第三个复选框</label>
</表单><脚本>var 扩展 = false;功能 showCheckboxes() {var checkboxes = document.getElementById("checkboxes");如果(!扩展){checkboxes.style.display = "block";扩展 = 真;} 别的 {checkboxes.style.display = "none";扩展 = 假;}}
解决方案
属性id"仅被 CSS 和 javascript 用于引用特定元素.你在这里想要的是给元素一个名字.像这样:
<label for="one"><input type="checkbox" name="one" id="one"/>第一个复选框</label><label for="two"><input type="checkbox" name="two" id="two"/>第二个复选框</label><label for="three"><input type="checkbox" name="three" id="three"/>第三个复选框</label>
然后您可以为 $_POST['one'] 或 $_POST['two'] 或 $_POST['three'] 检查您的 $_POST.它们应该完全丢失(使用isset())或应该作为值on"发布
if (isset($_POST['one']) && $POST['one'] === "on"){//为复选框一做事}
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>
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)
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>
解决方案
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
}
这篇关于mySqli php - 用户在 1 条记录中输入多项选择复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文