17
15

More than 5 years have passed since last update.

[PHP]POSTメソッドで値を投げる&投げた値を取得

Posted at

POSTメソッドで値を投げる

<form method="POST" action="hogehoge.php">
  <table>
    <tr>
      <th>rank</th>
      <th>artcle_id</th>
    </tr>
    <?php for ($i = 1; $i < 6; $i++) { ?>
    <tr>
      <td><?php echo $i; ?></td>
      <td><input type="text" name="<?php echo "article_id_" . $i ?>"</td>
    </tr>
    <?php } ?>
  </table>
  <input type="submit" value="変更">
</form>
  • formタグでinputタグたちを囲むべし
  • formタグのmethod属性でHTTPメソッドを指定
  • formタグのaction属性で送信先プログラムのURIを指定
  • inputタグのtype属性をtextにするとテキストボックス→テキスト入力欄
  • inputタグのname属性で書いた名前を用いて、後で値を受け取る
  • inputタグのtype属性をsubmitにすると、formタグで囲んだ内容を投げられる(←ボタン)
  • valueに書いた値がボタンに書かれる

投げた値を取得

$_POST[name]

nameのところは、上記「inputタグのname属性」に書いた名前に各自置き換えること。
上記の例では、

$_POST[$article_id_1]
$_POST[$article_id_2]
$_POST[$article_id_3]
$_POST[$article_id_4]
$_POST[$article_id_5]

とすることで、それぞれのテキストボックスに書いた文字列を取得できる。

17
15
3

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
17
15