11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

チェックボックスで複数選択されたデータを受け取る方法(PHP)

Last updated at Posted at 2017-03-15

■HTML

<form action="sample.php" method="POST">
    <p>好きな色(複数回答可): 
        <input type="checkbox" name="colors[]" value="white"><input type="checkbox" name="colors[]" value="black"><input type="checkbox" name="colors[]" value="red"><input type="checkbox" name="colors[]" value="blue"><input type="checkbox" name="colors[]" value="green"></p>
    <p><input type="submit" value="送信"></p>
</form>

■PHP

<?php
echo '<p>好きな色: ';
if (isset($_POST['colors']) && is_array($_POST['colors'])) {
  foreach( $_POST['colors'] as $value ){
      echo "{$value}, ";
  }
}
echo '</p>';
?>

(参考)
http://alphasis.info/2012/11/php-gyakubiki-form-post-multiple/

11
15
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?