0
1

More than 1 year has passed since last update.

【PHP】$_POSRで送信するためのチートシート

Posted at

$_POSTで送信するためには

発端

  • $_POSTでデータが欲しいがやり方が曖昧だったためまとめる。
    【結論】formにmethod="POST"buttonのtype="submit"にしてれば飛ぶ。

方法

form内で色々な情報を集めてまとめて送信する場合

index.php
<form action="" method="POST">
    // inputBox
    <input id="userId" type="text" name="userId">
    // password
    <input id="password" type="password" name="password">
    // adress
    <input id="adress" type="adress" name="adress">
    // selectBox
    <select name="gender">
         <option value="0">選択してください</option>
         <option value="1"></option>
         <option value="2"></option>
         <option value="3">その他</option>
    </select>
    // button
    <button type="submit" class="btn">登録</button>
</form>

POSTデータの取得

recieve.php
$userId = $_POST['userId'];
$password = $_POST['password'];
$adress = $_POST['adress'];
$gender = $_POST['gender'];

HOMEボタンやLOGOUTボタンのようにボタンのデータだけ飛ばしたい場合

index.php
<form method="post">
    <button type="submit" name="navber" value="home">HOME</button>
    <button type="submit" name="navber" value="logout">LOGOUT</button>
</form>

POSTデータの取得

recieve.php
    if($_POST['navber'] == "home"){
        // homeボタンが押されたときの処理
    }elseif($_POST['navber'] == "logout"){
        // logoutボタンが押されたときの処理
    }
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