MySQLi - JS - 下拉多选

时间:2017-10-03 18:32:27

标签: javascript php mysqli

初学者。
做了一个下拉菜单,你可以在我的数据库中选择3个选项 换句话说,我有3个属性记录" at1。 at2,at3"和下拉属性,如"红色,蓝色,黑色,橙色,绿色" (在下面的代码中,我刚刚将它们标记为"一,二,三和#34;)我想放入at1,at2,at3。

所以..到目前为止我有..

 $at0 = $_POST['at0'];
  $at1 = $_POST['at1'];
  $at2 = $_POST['at2'];

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

                      Attributes:
                        <div class="multiselect">
                          <div class="selectBox" onclick="showCheckboxes()">
                            <select>
                              <option>Select an option</option>
                            </select>
                            <div class="overSelect"></div>
                          </div>
                          <div id="checkboxes">
                            <input type="checkbox" name="at0" id="one" /> one
                            <label for="two"><input type="checkbox" name="two" id="two" />two
                            <label for="three"><input type="checkbox" name="three" id="three" />three




                          </div>
                        </div>

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

是否有更简单/更清晰的方法让用户选择多个选项,这些选项都放在我的数据库中? 目前它有点工作,当我点击第一个复选框时,单词&#34; On&#34;留在&#39; at1&#39;在我的数据库中..我如何得到我想要达到的目标?

1 个答案:

答案 0 :(得分:0)

https://developer.mozilla.org/es/docs/Web/HTML/Elemento/input/checkbox

中所述

如果选中,则输入复选框将默认为“on”。

您可以使用以下方式传递自定义值:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
    <ul ng-repeat="department in selectedDepartments">
        <li>
            <div>{{department.id}}</div>
            <input type="text" ng-model="department[name]" ng-value="department[name]"> {{department[name]}}
        </li>
    </ul>
</div>

如果勾选复选框,那将使POST或GET(取决于你的表格)var“at0”接收值“mycustomvalue”。

修改

如果您想要多个可以选中或不选中的复选框,可以使用数组语法作为名称。那就是:

HTML:

<input type="checkbox" name="at0" id="one" value="mycustomvalue" />

如果勾选方框1和3,则PHP部分将为

PHP:

<input type="checkbox" name="at0[]" id="one" value="valueone" />
<input type="checkbox" name="at0[]" id="two" value="valuetwo" />
<input type="checkbox" name="at0[]" id="three" value="valuethree" />
<input type="checkbox" name="at0[]" id="four" value="valuefour" />

然后你将如何将其保存到数据库中。只需记住清理输入以避免SQL注入攻击