荒屋敷智也のブログ

【CakePHP備忘録】入力FormのPost方法

当ブログではアフィリエイト広告を利用しています

「入力FormのGET」Viewソースコード

パターン1

<form action="/testcake/home/next" method="post">
    <input name="id" value="post_data">
    <input type="hidden" name="_csrfToken" value="<?= $this->request->getAttribute('csrfToken'); ?>">
    <input type="submit" value="更新" name="submit">
    <meta charset="utf-8">
</form>

パターン2

<?= $this->Form->create(null, ['url' => ['controller' => 'home', 'action' => 'next'], 'type' => 'post']) ?>
<?= $this->Form->input('id'); ?>
<?= $this->Form->submit('更新') ?>
<?= $this->Form->end() ?>

「入力FormのGET」Controllerソースコード

if ($this->request->is('post')) {
    //フォームから送信された値を取得。(inputのnameを参照)
    $this->set('id', $this->request->getData("id"));
} 

PostしたデータをViewへ出力

<p>id =<?= $id ?></p>