2011年4月7日木曜日

配列を理解する

■配列
input type="checkbox" name="arr[]" value="test1"
input type="checkbox" name="arr[]" value="test2"
input type="checkbox" name="arr[]" value="test3"
とすると、
arr[0] = test1
arr[1] = test2
arr[2] = test3
となる
submitで一括で送り、受け取り同様に
$POST_['arr'][0]
$POST_['arr'][1]
$POST_['arr'][2]
でとれる。


satart.php

6 <form method="post" action="next.php">
7
8 <?php
9 $a = 1;
10 while($a < 5){ ?>
11 <input type="checkbox" name="arr[]" value="<?php print $a; ?>"><?php print $a; ?>
12 <br />
13 <?php $a++;
14 }
15 ?>
16 <input type="submit" value="GO">
17 </form>


next.php

1 <?php
2
3 if(isset($_POST['arr'])) {
4 for ( $i = 0; $i < count( $_POST['arr']); $i++) {
5 echo $_POST['arr'][$i]."<br />";
6 }
7 }
8 ?>





■参考
PHPでフォームで送信されたチェックボックスの値を受け取るには?
http://kattsuk2.blog111.fc2.com/blog-entry-16.html

基本的なフォーム要素
http://www.kanzaki.com/docs/html/htminfo31.html

ソフィのphp入門:array()
http://nyx.pu1.net/reference/typearray/func_array.html

0 件のコメント:

コメントを投稿