【问题标题】:How to get two or more values of selected check boxes?如何获取选定复选框的两个或多个值?
【发布时间】:2014-05-13 21:41:19
【问题描述】:

我有一份商务经理职位的候选人名单。选民需要选择至少两名候选人。我使用复选框是为了让选民可以进行多项选择。 And when the voter clicks on next button to proceed voting to other positions , the two selected Business Manager should be carried..The problem is I can get the two but with the same Name.

例如:选民被选中

        John Smith and Harry Potter

我得到值时的结果只是最后一个被选中的人。我注意到在我的收件箱中我只使用了一个 name="choice" 所以不可能得到两个....当我的列表是静态的时,我不能为每个收件箱分配不同的名称,但在我的情况下,我使用了动态方式将它们放在复选框数组中,这意味着它们具有相同的 name="choice"...希望您理解我的意思...请帮助我..以下是我的代码实现..

  1. 填充复选框列表的结果:

        $result = mysql_query($QUERY);
                        while($row = mysql_fetch_object($result)){
                            $Name = $row->Name;
                            $PLName = $row->PLName;
                            $CID = $row->CID;
                            echo '<tr><td>';
                            echo '<img src="fetchImage.php?CID=' . $CID . ' " alt="Candidate photo" width="150px" height="135px" style="border:2px solid silver;">';
                            echo '</td>';
                            echo '<td><div class="contact_info_title">' . $Name . '</div><br>';
                            echo  $PLName . '<br><br>';
                            if(isset($_SESSION['VLPCCBusManager'])){
                                if($_SESSION['VLPCCBusManager'] == $CID){
                                    echo '<input type="checkbox" name="Choice" value=' . $CID . ' Checked>Select </td></tr>';
                                }else{
                                    echo '<input type="checkbox" name="Choice" value=' . $CID . '>Select </td></tr>';
                                }
                            }else{
                                echo '<input type="checkbox" name="Choice" value=' . $CID . '>Select </td></tr>';
                            }
                        }
                    echo '';
    
  2. 点击提交时获取选择的选项移动到另一个表单:

    if(isset($_POST['submit'])){
    if(isset($_POST['Choice'])){
        if(empty($_POST['Choice'])){ $errors='Choice'; }
        if(empty($errors)){ 
            $Choice = $_POST['Choice'];
            $_SESSION['VLPCCBusManager'] = $Choice;
            $_SESSION['VLPCCBusManager2'] = $Choice;
            header("Location: VLPCCSgtArms.php?ChoiceCat=$ChoiceCat");
        }
    }else{
        $_SESSION['VLPCCBusManager'] = '0';
        $_SESSION['VLPCCBusManager2'] = '0';
        header("Location: VLPCCSgtArms.php?ChoiceCat=$ChoiceCat");
    }
    

    }

【问题讨论】:

    标签: php session select checkbox


    【解决方案1】:

    您需要将复选框的名称指定为:

    <input type="checkbox" name="choice[]" />
    

    而不仅仅是

    <input type="checkbox" name="choice" />.
    

    现在,例如,你在 HTML 中有这个:

    <input type="checkbox" name="choice[]" value="choice1"> Choice1
    <input type="checkbox" name="choice[]" value="choice2"> Choice2
    <input type="checkbox" name="choice[]" value="choice3"> Choice3
    

    假设你勾选了第一个和第三个复选框,

    在 PHP 中,您可以将复选框勾选为:

    <?php
    
    print_r($_POST['choice']);
    
    ?>
    

    输出将是:

    Array('0' => 'choice1',
          '1' => 'choice3')
    

    我希望这能解释你的疑问。

    【讨论】:

      【解决方案2】:

      您需要将复选框的名称指定为数组:

      然后你得到数组中的值,然后你可以保存在数据库中

      您可以以不同的方式提及复选框名称,例如 1、2、3、4、5、6.. 并使用表单发布值并将其作为数组获取 例如

      $values[0]=$_POST['1'];
          $values[1]=$_POST['2'];
          $values[2]=$_POST['3'];
          $values[3]=$_POST['4'];
          $values[4]=$_POST['5'];
          $values[5]=$_POST['6'];
          $values[6]=$_POST['7'];
          $values[7]=$_POST['8'];
          $values[8]=$_POST['9'];
          $values[9]=$_POST['10'];
      
          and then u can implode it to a variable and save it in the database 
      
          $skill_set = implode(",", $values);
      

      【讨论】:

      • 我不熟悉动态形式的数组。你能举个例子来解决我的问题吗...
      【解决方案3】:

      您必须将代码更改为

      name="choice[]"
      

      【讨论】:

        【解决方案4】:

        从复选框中获取多个值

        1. 您需要将复选框的名称更改为这样的数组
        <input type="checkbox" name="Choice[]" />
        
        1. 在读取复选框的帖子值时,如下所示

        $_POST['Choice'][0],$_POST['Choice'][1],...

        示例

         <input type="checkbox" name="Choice[]" value=" Checkbox 1"/> Checkbox 1<br>
         <input type="checkbox" name="Choice[]" value=" Checkbox 2"/> Checkbox 2<br>
         <input type="checkbox" name="Choice[]" value=" Checkbox 3"/> Checkbox 3<br>
        

        如果您只选中了复选框 1 和 3 ,那么您可以获得这样的值

        $_POST['Choice'][0] => 具有已选中的第一个复选框的值。

        所以$_POST['Choice'][0] 的值为Checkbox 1

        $_POST['Choice'][1] => 具有已选中的第二个复选框的值。

        所以$_POST['Choice'][1] 的值为Checkbox 3

        【讨论】:

        • 当我点击任何一个复选框时,如何读取正确的 POST 索引?
        • 如果你只选中了一个复选框,那么你需要只读$_POST['Choice'][0]
        • 如果任何一个复选框没有被选中,它就不会出现在$_POST 数组中。例如,您有 3 个复选框。如果你检查checkbox 1checkbox 3,那么它们对应的值将分别在$_POST['Choice'][0]$_POST['Choice'][1]
        • 我撤回了我的最后一个请求..我分析了你上面的样本,我明白了逻辑......我真的做到了......是的......我非常感谢这件事...... .thanks..我将这两个变量替换为 $_SESSION['VLPCCBusManager'] = $_POST['Choice'][0]; $_SESSION['VLPCCBusManager2'] = $_POST['Choice'][1];
        • 很高兴能为您提供帮助。如果我的回答解决了你的问题,你可以接受
        猜你喜欢
        • 2015-02-11
        • 1970-01-01
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 2011-07-17
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多