为了账号安全,请及时绑定邮箱和手机立即绑定

是否可以通过表单提交保存数​​据

是否可以通过表单提交保存数​​据

PHP
守着一只汪 2022-07-22 17:08:08
控制器代码:    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();?>我不明白数据已提交给控制器,但要从控制器读取数据。我用控制器保存了该数据,但要访问数据变量,即提交无法读取到控制器的表单数据。
查看完整描述

1 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

要获取发布数据,请使用调试


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'));

    }

你需要更多吗?只需要在你的应用文件夹中给出一个简单的命令来生成你的 CRUD。


发号施令


bin/cake bake all users

假设,users 是你的数据库表名。然后你会得到添加,编辑,删除,查看。 可以看这个教程看看如何给蛋糕烘烤命令


查看完整回答
反对 回复 2022-07-22
  • 1 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号