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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条