0
2

More than 3 years have passed since last update.

PHPでの入力フォーム作成

Posted at

PHPでの入力フォーム作成について

以下のような入力フォームをPHPで作成し、
別ファイルへ出力するようにした。

スクリーンショット 2020-12-03 20.20.14.png

ソースコード(ファイル名index.php)

<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form action="submit.php" method="get" >
<p> 名前: <input type="text" name="your_name" value=""></p>
<p> 年齢: <input type="text" name="your_age" value=""></p>
<input type="submit" >
</form>
</body>
</html>

名前と年齢を入力して、送信ボタンを押すと以下の通り、出力される(出力ファイル名:submit.php)

スクリーンショット 2020-12-03 20.23.20.png

スクリーンショット 2020-12-03 20.25.28.png

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<p><?php echo $_POST["your_name"] ?>です</p>

<p>年齢は<?php echo $_POST["your_age"] ?>です</p>
</body>
</html>

今回はGETでサンプルコードを記載したが、GETをPOSTに変更すれば同様に出力される。

GETとPOSTの違い

GETについて

GETでデータを取得した場合、URLの後ろに送りたいデータを付与して、送信する。
以下の通り、入力した名前と年齢がURLに反映されている。

GETを使う場合、URLにデータが残ってしまうというデメリットあり

スクリーンショット 2020-12-03 20.28.44.png

POSTについて

POSTを使用する場合、HTTPリクエスト内のメッセージボディにデータを含めて送信する。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<form action="submit.php" method="post" >
<p> 名前: <input type="text" name="your_name" value=""></p>
<p> 年齢: <input type="text" name="your_age" value=""></p>
<input type="submit" >

</form>
</body>
</html>

postでデータを取得した場合、URLには送信したデータが記載されない。
URLに記載されるべきではない情報(PW等のログイン情報)がPOSTで送信される。

スクリーンショット 2020-12-04 5.34.06.png

0
2
0

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
0
2