0
0

More than 1 year has passed since last update.

filter_input関数(メソッド)の分かりやすい?記事

Last updated at Posted at 2023-07-19

概要

filter_inputってどうやって渡ってくるんだっけ?
忘れちゃうなーって思ったので記事にしました。

バージョン

  • php 8.1

html

<form action="signin_post.php" method="post">
                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" class="form-control" id="name" name="name">
                    </div>
                    <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" id="password" name="password">
                    </div>
                    <button type="submit" class="btn btn-secondary">Sign in</button>

こちらはphpファイルでhtmlを書いています。

ログインでボタンを押すと処理が流れ、signin_post.phpに情報が渡ります。

filter_input

filter_inputは

INPUT_GET、INPUT_POST、 INPUT_COOKIE、INPUT_SERVER INPUT_ENV のうちいずれかを使いますが、
最初はINPUT_GET、INPUT_POSTだけ覚えておけば大丈夫です。

filter_input(INPUT_POST,"name");

早速INPUT_POSTを使用しています。

カンマのあとに来ている"name"は、

<input type="text" class="form-control" id="name" name="name">

のnameのnameを取得します。(ややこしい)

passwordならば

filter_input(INPUT_POST,"password");

nameのpasswordを取得します。

<input type="text" class="form-control" id="password" name="password">

取得してからどう動くか

現在filter_inputは、htmlから送られた値を持っています。

例えば上記のhtmlで、emuriと打てば、filter_input関数は、emuriという値を返します。

それを

$name = filter_input(INPUT_POST,"name");

$nameに持たせることもできます

値を確認するときはvar_dumpを使って値が来ているか確認しましょう

結論

関数はよく忘れるので、イメージをしながら復習をするのが一番です。
そうすると頭に定着しやすいですからね

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