Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

フォームの作成(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));
        }
    }
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした