LoginSignup
0
0

More than 5 years have passed since last update.

CakePHP3 フォームの利用について

Last updated at Posted at 2017-08-17

フォームの作成(GETパターン)

name="text1"のvalueを格納する方法

//get方式でnameがtext1のデータを受け取る記述
$str = $this->request->query['text1'];
/src/Template/Hello/index.ctp

<h1>サンプル見出し</h1>
<form action="hello/sendForm" method="get">
    <input type="text" name="text1">
    <input type="submit">
</form>


HelloController.php
<?php 
    namespace App\Controller;

    class HelloController extends AppController {
        // クラス全体に適用される設定 initializeメソッドはクラスのインスタンスが作成される際に自動的に呼び出される
        public function initialize(){
            $this->viewBuilder()->layout('hello');
            $this->set('msg','Hello/index');
            $this->set('footer','Hello/footer2');

        }

        public function sendForm() {
            $str = $this->request->query['text1'];
            //こちらでも大丈夫 $str = $this->request['url']['text1'];
            $result = "";
            if($str !="") {
                $result = "you type:".$str;
            } else {
                $result = "empty.";
            }
            $this->set("str",$str);
            //htmlspecialchars(string)のショートハンド関数
            $this->set("result",h($result));
        }
    }
/src/Template/Hello/send_form.ctp (sendForm.ctpでないことに注意)
<h1>送信結果</h1>
<p><?=$result ?></p>

一括でデータを出力する方法

/src/Template/Hello/index.ctp、/src/Template/Hello/send_form.ctp は同じ

HelloController.php
<?php 
    namespace App\Controller;

    class HelloController extends AppController {
        // クラス全体に適用される設定 initializeメソッドはクラスのインスタンスが作成される際に自動的に呼び出される
        public function initialize(){
            $this->viewBuilder()->layout('hello');
            $this->set('msg','Hello/index');
            $this->set('footer','Hello/footer2');

        }

        public function sendForm() {
            $result = "送信された情報";
            foreach($this->request->query as $key => $val) {
                $result .= $key . "=>".$val."<br>";
            }
            $this->set("result",$result);
        }
    }
送信結果

送信された情報
check1=>on
radio1=>on
radio2=>on
text1=>あ
select1=>Windows

フォームの作成(POSTパターン)

//post方式でnameがtext1のデータを受け取る記述
 $str = $this->request->data['text1'];
index.ctp
<h1>サンプル見出し</h1>
<form action="hello/sendForm" method="post">
    <input type="text" name="text1">
    <br><br>
    <input type="submit">
</form>
HelloController.php
<?php 
    namespace App\Controller;

    class HelloController extends AppController {
        // クラス全体に適用される設定 initializeメソッドはクラスのインスタンスが作成される際に自動的に呼び出される
        public function initialize(){
            $this->viewBuilder()->layout('hello');
            $this->set('msg','Hello/index');
            $this->set('footer','Hello/footer2');

        }

        public function sendForm() {
            $str = $this->request->data['text1'];
            $result = "";
            if($str !="") {
                $result = "you type:".$str;
            } else {
                $result = "empty.";
            }
            $this->set("str",$str);
            //htmlspecialchars(string)のショートハンド関数
            $this->set("result",h($result));
        }
    }
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