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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法