문제

First page form

<form action="calculatepage.php" method="post">
<table width="200" border="1">
  <tr>
    <td><?php echo $row['dvd_copy_id']?></td>
    <td><?php echo $row['dvd_title']?></td>
    <td><?php echo $row['price']?></td>
    <td><input type="checkbox" value="<?php echo $row['dvd_copy_id']?>" name="checkbox"></td>
  </tr>
</table>

<p>

  <?php
        }
    ?>
      <input type="submit" name="button" id="button" value="Submit" />
   </form>

Second page

<?php

echo $_POST['checkbox']; //This shows up if the last checkbox is checked

foreach($_POST['checkbox'] as $value) {
    echo print_r($value);
}
 ?>

It returns error "Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test\calculatepage.php on line 15" at the second page.

How to fix this and show multiple checkbox values input from the first page? Any help would be appreciated.

도움이 되었습니까?

해결책

Replace your line with the following Check here name="checkbox[]"

<td><input type="checkbox" value="<?php echo $row['dvd_copy_id']?>" name="checkbox[]"></td>

And echo print_r($value); is wrong. You should use print_r(); or var_dump(); for an array

다른 팁

A form will only post checkboxes when they are checked. So if the user doesn't check anything, it will not post anything. You can fix that quite easily by inserting an empty array if nothing is posted.

AND you need to change the name to checkbox[], otherwise it'll just post a string (or nothing).

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

$checkboxes = is_array($_POST['checkbox']) ? $_POST['checkbox'] : array();
foreach($checkboxes as $checkbox) {
    // ...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top