【问题标题】:counting how many checkbox are checked php/html计算检查了多少个复选框 php/html
【发布时间】:2013-09-05 12:16:38
【问题描述】:

您好,我是 php 新手,我想知道单击提交后如何计算有多少 'checkbox' 被选中。 例如:

<input type = "checkbox" value = "box" name = "checkbox1"/>
<input type = "checkbox" value = "box" name = "checkbox2"/>
<input type = "checkbox" value = "box" name = "checkbox3"/>

【问题讨论】:

    标签: php html checkbox


    【解决方案1】:

    将复选框命名为数组

    <input type = "checkbox" value = "box" name = "checkbox[]"/>
    

    然后提交试试喜欢

    $checked_arr = $_POST['checkbox'];
    $count = count($checked_arr);
    echo "There are ".$count." checkboxe(s) are checked";
    

    注意:并根据您的表单提交使用的方法...无论是$_GET 还是$_POST,您都需要使用$_POST['checkbox'] 进行POST 方法,$_GET['checkbox'] 用于 GET 方法。

    【讨论】:

    • 这可能是正确的答案,因为不使用name = "checkbox[]" 它需要检查多个值。
    • 如果输入类型是“数字”输入类型怎么办
    • 呵呵,看来我应该只是发布我的答案而不是花时间测试它。和你的答案一样。 +1
    【解决方案2】:

    当您单击提交时,所有选中的框都将在请求中。 在你的情况下,如果 checkbox1 被选中,你会得到: "checkbox1=box"

    如果你使用 GET 作为方法,它看起来像:http://yoururl.com/yourcode.php?checkbox1=box 并且你可以使用 $_GET['checkbox1']

    如果您使用 POST 作为方法,您可以使用 $_POST['checkbox1']

    您还可以使用 isset($_POST['checkbox1']) 检查该框是否已选中(以及在请求数据中)

    【讨论】:

      【解决方案3】:

      您必须重命名并添加值

      <input type = "checkbox" value = "box" name = "checkbox[]" value="1"/>
      <input type = "checkbox" value = "box" name = "checkbox[]" value="2"/>
      <input type = "checkbox" value = "box" name = "checkbox[]" value="3"/>
      

      这样你不仅会知道数字(你实际上并不需要)

      echo count($_POST['checkbox']);
      

      但也有实际选择的值:

      foreach($_POST['checkbox'] as $val)
      {
          echo "$val<br>\n";
      }
      

      【讨论】:

        【解决方案4】:

        您可以将复选框的名称设置为数组:

        <input type = "checkbox" value = "box" name = "checkbox[1]"/>
        <input type = "checkbox" value = "box" name = "checkbox[2]"/>
        <input type = "checkbox" value = "box" name = "checkbox[3]"/>
        

        然后您将在 PHP 中拥有一个数组 ($_POST['checkbox']):

        echo count( $_POST['checkbox'] ); // this will give you the count
        

        否则你可以遍历它们中的每一个并增加一个变量:

        $counter = 0;
        foreach( array('checkbox1', 'checkbox2', 'checkbox3') as $name ) {
          if( isset( $_POST[ $name ] ) {
             $counter++
          }
        }
        echo $counter;
        

        【讨论】:

          【解决方案5】:
          <input type = "checkbox" value = "box" name = "checkbox"/>
          <input type = "checkbox" value = "box" name = "checkbox"/>
          <input type = "checkbox" value = "box" name = "checkbox"/>
          

          要检查哪些框已被选中,只需遍历 chk[] 像这样的数组:

          $chk_array = $_POST['checkbox'];
          
          for($chk_array as $chk_key => $chk_value)
          {
          print 'Checkbox Id:'. $chk_key . ' Value:'. $chk_value .'is
          checked';
          }
          

          【讨论】:

            【解决方案6】:
            $checkedBoxes = 0;
            
            // Depending on the action, you set in the form, you have to either choose $_GET or $_POST
            if(isset($_GET["checkbox1"])){
              $checkedBoxes++;
            }
            if(isset($_GET["checkbox2"])){
              $checkedBoxes++;
            }
            if(isset($_GET["checkbox3"])){
              $checkedBoxes++;
            }
            

            【讨论】:

            • 这是一个很棒的。真正的 PHP 方式。
            【解决方案7】:

            使用jQuery你可以实现它:

            $("input:checkbox:checked").length

            这将返回选中的复选框数。

            而在 php 中,您需要将其作为数组传递。

            echo count($_POST['checkbox']);

            【讨论】:

              猜你喜欢
              • 2019-02-10
              • 1970-01-01
              • 2013-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-06-15
              相关资源
              最近更新 更多