Several Checkboxes sharing the same name

前端 未结 2 964
猫巷女王i
猫巷女王i 2021-02-19 07:14

According to the w3c \"Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property.\"

相关标签:
2条回答
  • 2021-02-19 08:08

    That would never have worked without the [], not in PHP.

    W3C don't specify anything about how query strings are handled server-side. (Ignoring an irrelevant, obsolete corner of the CGI spec, only relevant to PHP in that it was a security hole up until recently).

    It looks like that pattern is valid markup, but not commonly used, for the reason you describe.

    A similar pattern is used for radio buttons, of which only one can be selected at a time. (In fact, giving the radio inputs the same name is how the browser knows to treat them as a group). Perhaps that's what you were thinking of.

    0 讨论(0)
  • 2021-02-19 08:08

    If you really want it in PHP, try this:

    <?php
    
    if (count($_POST)) {
      header("Content-type: text/plain");
      $fp = fopen("php://input", "r");
      fpassthru($fp);
      fclose($fp);
      exit;
    }
    
    ?>
    <form action="" method = "post">
    <input type="checkbox" name="pet" value="dog" />Dog<br />
    <input type="checkbox" name="pet" value="Cat" />Cat<br />
    <input type="checkbox" name="pet" value="bird" />bird<br />
    <input type="checkbox" name="pet" value="iguana" />iguana<br />
    <input type="submit" />
    
    </form>
    

    More on php://input stream can be found in PHP documentation.

    0 讨论(0)
提交回复
热议问题