LoginSignup
9
10

More than 5 years have passed since last update.

PHP セレクトボックスはfor,while,foreachを使っても作れる

Last updated at Posted at 2016-10-31

■test05.php 入力画面
■foreach構文版

    <form action="test05_output.php" method="post">
        <p>
            <select name="number">
            <?php
            $number = array (
                    "10",
                    "20",
                    "30",
                    "40",
                    "50"
            );
            foreach ( $number as $value ) {
                echo '<option value="', $value, '">', $value, '</option>';
            }
            ?>
            </select>
        </p>

        <input type="submit" value="送信">

■for構文版

<body>

    <form action="test05_output.php" method="post">
        <p>
            <select name="number">
        <?php
        for($number = 10; $number <= 50; $number = $number + 10) {
            echo '<option value="', $number, '">', $number, '</option>';
        }
        ?>
        </select>
        </p>

        <input type="submit" value="送信">

    </form>

</body>

■while版

<body>

    <form action="test05_output.php" method="post">
        <p>
            <select name="number">
            <?php
            $number = 10;
            while ( $number <= 50 ) {
                echo '
                <option value="', $number, '">', $number, '</option>';
                $number = $number + 10;
            }
            ?>
            </select>
        </p>

        <input type="submit" value="送信">

    </form>

</body>

■test05_output.php 結果画面

<?php
$number = $_POST ['number'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xm1ns="http://www.w3.org/1999/xhtml">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript">


    <title>PHP入門</title>

</head>
<body>
<?php
echo $number;
?>

</body>
</html>
9
10
1

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
9
10