douqingzhi0980 2019-08-09 14:58
浏览 136
已采纳

在PHP中收集多个复选框的值(2以相同的形式提交?)

I have two submits in the same form. One to filter the mySQL db and generate a list of checkboxes (works fine) and the other to display the values of the selected items using PHP (no go). Later on, I'll store these values to built a sql query. Please visit https://wintoweb.com/sandbox/question.php

I've tried everything you can imagine. Going nuts...

PHP function called from the [Show selected] button :

<?php      
function getNames() {
    //echo 'GOT IT !';              
    if(isset($_GET['choices[]'])){
        if(!empty($_GET['choices'])){
            foreach($_GET['choices'] as $selected){
                echo $selected."</br>";
            }
        }
    }
}
?>

Code for the [Show selected] button :

EXPECTED : the list of selected values below the ACTUAL OUTPUT : no list of selected values. The PHP function getNames() is never traversed.

  • 写回答

1条回答 默认 最新

  • douxiong2001 2019-08-09 15:18
    关注

    I simplified your form.

    <form name="dig" action="#" method="get">
    <input type="search" name="l_name" value="Author's name (whole or part)" style="width:200px;" onfocus="this.value=''" onblur="(this.value=='')? this.value='Author's name (whole or part)':this.value;" />
    <input type="submit" name="search" value="Search"> 
    <input type="submit" name="display_all" value="Show selected">
    <br /><br />
    <table style="width:400px">
        <tr>
            <td width="100%"><input type="checkbox" name="choices[]" value="ABÉLARD, Pierre et HÉLOÏSE" onclick="">ABÉLARD, Pierre et HÉLOÏSE</td>  
        </tr>
        <tr>
            <td width="100%"><input type="checkbox" name="choices[]" value="ABOUT, Edmond" onclick="">ABOUT, Edmond</td>  
        </tr>
        <tr>
            <td width="100%"><input type="checkbox" name="choices[]" value="ABRANTÈS, Laure Junot" onclick="">ABRANTÈS, Laure Junot</td>  
        </tr>
    </table>
    </form>
    
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    
    if(isset($_GET['search'])){
        echo 'Search</br>';
    } elseif(isset($_GET['display_all'])) {
        echo getNames();
    }
    
    function getNames() {
        $rets = '';
        if(isset($_GET['choices']) and !empty($_GET['choices'])){
          foreach($_GET['choices'] as $selected){
            $rets .= $selected.'</br>';
          }
        }
        return $rets;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络