簡體   English   中英

是否可以通過表單提交保存數​​據

[英]Is that possible to save data with form submission

控制器代碼:

    public function login()
    {
        $this->LoadModel('Users');

        pr($this->data);
    }

查看代碼:

<?php echo
$this->Form->create('Post').
$this->Form->control('email').'<br>'.
$this->form->label('password').'<br>'.
$this->Form->password('password').'<br>'.
$this->Form->submit('log in').
$this->Form->end();
?>

我不明白數據提交給控制器而是從控制器讀取數據。 我用控制器保存了該數據,但要訪問數據變量,即提交無法讀取到控制器的表單數據。

要獲取發布數據,請使用調試

debug($this->request->getData()); 

看起來您需要所有 CRUD 代碼。

因此,您可以在下面創建您的用戶 add.ctp 文件

位置:模板\\用戶\\add.ctp

<?= $this->Form->create($user) ?>

<?php
   echo $this->Form->control('username');
   echo $this->Form->control('email');
   echo $this->Form->control('password');
?>

<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

然后用戶控制器看起來像

 UsersController/add method look like 

 public function add()
    {
        $user = $this->Users->newEmptyEntity();
        if ($this->request->is('post')) {
            $user = $this->Users->patchEntity($user, $this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
        $this->set(compact('user'));
    }

你需要更多嗎? 只需要在你的 app 文件夾中給出一個簡單的命令來生成你的 CRUD。

下達命令

bin/cake bake all users

假設,users 是你的數據庫表名。 然后你會得到添加、編輯、刪除、查看。 可以查看本教程以了解如何發出蛋糕烘焙命令

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM