dougutuo9879 2016-05-10 14:09
浏览 44
已采纳

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

I want to put all values from selected checkboxes into its own PHP variable. These checkboxes have their values generated by PHP/SQL. This same variable has to be shown in a simple <div> below the dropdown boxes. I am also using Bootstrap Multiselect Plugin. But I get a null value from my var_dump() test.

here is what I have attempted so far:

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

I can however accomplish what I want to do by using jQuery but I want to be able to send those values to a database and my tutor wants them to be in PHP variables. I know I can use AJAX to send them to my database but this is what my tutor wants instead. I don't know if I am on the right track, so any help is greatly appreciated

  • 写回答

1条回答 默认 最新

  • duandan4680 2016-05-10 14:25
    关注

    At the very first run you wont read anything from that POST variable, so handle it. Try this code, it should work:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?