PHP处理多个复选框

Jus*_*oss 1 php-5.3

好的,我的index.html表格如下:

<form action="process.php" method="post">
  <table>
    <tr>
      <td><input name="Field[]" type="checkbox" value="Accounting" />Accounting</td>
      <td><input name="Field[]" type="checkbox" value="Finance" />Finance</td>
      <td><input name="Field[]" type="checkbox" value="Marketing" />Marketing</td>
    </tr>
  </table>
</form>
Run Code Online (Sandbox Code Playgroud)

我有process.php如下:

<table>
  <tr>
    <th>Field(s):</th>
    <td>
      <?php
        if(isset($_POST['Field']))
        {
          for($i = 0; $i < count($_POST['Field']); $i++)
          { echo $_POST['Field'][$i] . ' '; }
        }
      ?>
    </td>
  </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我只得到打印出来的最后一个复选框的第一个字母.请帮忙!

muh*_*sil 5

Try this one in process.php to get the values from $_POST['Field']

   <table>
    <tr>
     <th>Field(s):</th>
     <td>
      <?php
        if(isset($_POST['Field']))
        {
          foreach ($_POST['Field'] as $value) {
            echo $value;
          }
        }
      ?>
     </td>
    </tr>
   </table>
Run Code Online (Sandbox Code Playgroud)

  • 它不会影响javascript代码,因为process.php在服务器端运行.请仔细检查index.html中的javascript代码或在此处发布index.html代码.所以有人可以检查它,并为您提供一个清晰的解释. (2认同)