LoginSignup
0
1

More than 3 years have passed since last update.

入力内容の取得

Last updated at Posted at 2020-09-03

出力側

出力する側のhtmlのform属性のactionに送信先のphpファイルを設定する。

qiita.html
<form action="submit.php" method="get">
        <label for="my_name">氏名:</label>
        <input type="text" id="my_name"
               name="my_name" maxlength="255"
               value="">
        <input type="submit" value="送信する">
    </form>

受け取る側

$_REQUESTの内容はname属性でつけた値を入れることで取得

htmlspecialcharsとすることでhtmlのタグとして認識される文字列を逃がせる

ENT_QUOTESは数値であるために””はつかない

qiita.php
<?php
        print(htmlspecialchars
        ($_REQUEST['my_name'],ENT_QUOTES));
        ?>

getメソッド以外のやり方

method = postを指定する
このやり方だとURLは変化しない

qiita.html
<form action="submit.php" method="post">
        <label for="my_name">氏名:</label>
        <input type="text" id="my_name"
               name="my_name" maxlength="255"
               value="">
        <input type="submit" value="送信する">
    </form>

submit.phpのほうは$_POSTを使う

$_REQUESTを使えば、汎用的にget,postどちらも受け取れるが思っても見ないような操作が発生する場合があるので注意

qiita.php
<?php
        print(htmlspecialchars
        ($_POST['my_name'],ENT_QUOTES));
        ?>

ラジオボタン

php側に送られるのはvalue属性で設定した値、
次の場合、男性を選択しても送られる値はvalueで設定したmale

qiita.html
<form action="submit.php" method="post">
        <p>性別:
            <input type="radio" name="gender" value="male">男性
            /
            <input type="radio" name="gender" value="female">女性
        </p>
            <input type="submit" value="送信する">
    </form>

複数の値を取得

name属性に[]をつけることで複数選択を受け取れるようにする

qiita.html
<form action="submit.php" method="post">
        <p>
            <input type="checkbox" name="reserve[]" value="1/1">1月1日<br>
            <input type="checkbox" name="reserve[]" value="1/2">1月2日<br>
            <input type="checkbox" name="reserve[]" value="1/3">1月3日<br>

        </p>
            <input type="submit" value="送信する">
    </form>

checkboxの値は配列の形で出力されるためforeach/for構文を使う

qiita.php
<?php
        foreach ($_POST['reserve'] as $reserve){
            print (htmlspecialchars($reserve,ENT_QUOTES). ',');
        }
        ?>
0
1
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
1