将选中的复选框放入PHP变量中,然后我可以直接在下面查看


Getting selected checkboxes into a PHP variable, that I can then view directly below

我想将所选复选框中的所有值放入自己的PHP变量中。这些复选框的值由PHP/SQL生成。同样的变量必须显示在下拉框下方的简单<div>中。我也在使用Bootstrap Multiselect插件。但我从var_dump()测试中得到了一个null值。

以下是我迄今为止所尝试的:

<form id="menu1" method="POST" action="index.php">
    <h2>Area Code</h2>
    <select id="multi-select1" name="multi_select1[]" multiple="multiple">
        <?php
            //The query asking from our database
            $areaCodeSQL = "SELECT ac.Number AS `AreaCode`, ac.Name AS `AreaName`
                            FROM `AreaCodes` ac";                                                               //SQL query: From the table 'AreaCodes' select 'Number' and put into 'AreaCode', select Name and put into 'AreaName'
            $areaCodeResults = $conn->query($areaCodeSQL);                                                      // put results of SQL query into this variable
            if ($areaCodeResults->num_rows > 0) {                                                               // if num_rows(from $results) is greater than 0, then do this:
                // output data of each row
                            foreach($areaCodeResults as $areaCodeResult)                                        //for each item in $areCodeResults do this:
                                {
                                    $areaNameAndCode =  $areaCodeResult['AreaCode'] ." ". $areaCodeResult['AreaName'];  //get AreaCode and AreaName from query result and concat them
                                    $areaName = $areaCodeResult['AreaName'];                                    // get AreaName
                                    $areaCode = $areaCodeResult['AreaCode'];                                    //get AreaCode
                                    ?><option class="menuoption1" name="menuAreaCode" value="<?php echo $areaCode ?>"  ><?php echo $areaNameAndCode; ?></option><?php  //Create this option element populated with query result variables
                                }
            } 
            $result = $_POST['multi_select1'];                   
        ?>
    </select>   
</form>
<div id="showResults1"><?php var_dump($result) ?></div>

然而,我可以通过使用jQuery来完成我想要做的事情,但我希望能够将这些值发送到数据库,我的导师希望它们在PHP变量中。我知道我可以使用AJAX将它们发送到我的数据库,但这正是我的导师想要的。我不知道我是否走上了正确的道路,所以非常感谢

的任何帮助

在第一次运行时,您不会从POST变量中读取任何内容,所以请处理它。试试这个代码,它应该工作:

<form id="menu1" method="POST">
    <h2>Area Code</h2>
    <select id="multi-select1" name="multi_select1[]" multiple="multiple" action="index.php">
        <?php
            //The query asking from our database
            $areaCodeSQL = "SELECT ac.Number AS `AreaCode`, ac.Name AS `AreaName`FROM `AreaCodes` ac";                                                               //SQL query: From the table 'AreaCodes' select 'Number' and put into 'AreaCode', select Name and put into 'AreaName'
            $areaCodeResults = $conn->query($areaCodeSQL);                                                      // put results of SQL query into this variable
            if ($areaCodeResults->num_rows > 0) {                                                               // if num_rows(from $results) is greater than 0, then do this:
            // output data of each row
                foreach($areaCodeResults as $areaCodeResult) { ?>
                    <option class="menuoption1" name="menuAreaCode" value="<?php echo $areaCodeResult['AreaCode'] ?>"  ><?php echo $areaCodeResult['AreaCode'] ." ". $areaCodeResult['AreaName']; ?></option>
            <?php } ?>
    </select>   
    <input type="submit" value="Send" />
</form>
<div id="showResults1">
    <?php 
    $result = isset($_POST['multi_select1']) ? $_POST['multi_select1'] : [];
    var_dump($result);
    ?>
</div>
相关文章: