PHP vol.14

  • POSTを使ってみる

POSTを使ってみる。

<html>
<head>
<title>sample1</title>
</head>
<body>
<form action="./sample1.php" method="post">
<input type="text" name="text" />
<input type="submit" />
</form>
<?php
     echo $_POST['text'];
?>
</body>
</html>
$_POST[name] POSTの値が取得できる
$_GET[name] GETの値が取得できる

<html>
<head>
<title>sample2</title>
</head>
<body>
<form action="./sample2.php" method="get">
<input type="text" name="text" />
<input type="submit" />
</form>
<?php
     echo $_GET['text'];
?>
</body>
</html>

$_GETにするとURL部分に出る。
example.php?name=text

前回の九九の問題を弄ってみる

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>SHOP99</title>
</head>
<body>
<form action="./sample4.php" method="get">
     <input type="text" name="gyo" />
     ×
     <input type="text" name="retsu" />
     <input type="submit" value="=" />
</form>

<table border="1">
<?php
for ($k=1;$k<=$_GET['gyo'];$k++) {
    echo "<tr>\n";
    echo "<th>".$k."の段</th>\n";
    for ($i=1;$i<=$_GET['retsu'];$i++) {
        echo "<td>".$k."×".$i."=".($k*$i)."</td>\n";
    }
    echo "</tr>\n";
}
?>
</table>
</body>
</html>

上記のコードにマイナスの値と文字列と0を弾く

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>SHOP99</title>
</head>
<body>
<form action="./sample4.php" method="get">
     <input type="text" name="gyo" value="<?php echo $_GET['gyo']?>" />
     ×
     <input type="text" name="retsu" value ="<?php echo $_GET['retsu']?>" />
     <input type="submit" value="=" />
</form>

<?php
     if (empty($_GET['gyo']) || empty($_GET['retsu'])) {
         echo "値を入力してください。";
     } elseif ($_GET['gyo'] < 0 || $_GET['retsu'] < 0) {
             echo "マイナスの値は入力できません。";
     } elseif (is_numeric($_GET['gyo']) && is_numeric($_GET['retsu'])) {
             echo "<table border='1'>";
             for ($k=1;$k<=$_GET['gyo'];$k++) {
                 echo "<tr>\n";
                 echo "<th>".$k."の段</th>\n";
                 for ($i=1;$i<=$_GET['retsu'];$i++) {
                     echo "<td>".$k."×".$i."=".($k*$i)."</td>\n";
                 }
                 echo "</tr>\n";
             }
             echo "</table>";
         } else {
             echo "文字列は受け付けられません。";
         }
?>
</body>
</html>