【问题标题】:How to submit a form from an element to a controler and store it in another model如何将表单从元素提交到控制器并将其存储在另一个模型中
【发布时间】:2013-05-26 00:32:50
【问题描述】:

我有一个控制器 Users_controller 和一个函数 travel()。我有一个用于该函数的视图 travel.ctp。在 travel.ctp 我使用一个元素 profile.ctp 来显示存储在视图/元素/中的一些内容个人资料.ctp。 在 travel.ctp 中我添加了元素

  <?php echo $this->element("profile");?>

在 profile.ctp elemet 中,我编写如下表单操作

    <?php echo $this->Form->create('User', array('url' => array('action' => 'travel')));?>

我的怀疑是

  1. 如何在 travel() 中获取表单数据?
  2. 如何将该数据保存在另一个表中?
  3. 如何重用该元素?因为我为该表单提交编写了一个动作,那么我如何将它重用于另一个动作?

【问题讨论】:

    标签: forms cakephp


    【解决方案1】:

    在你的控制器中你必须设置一个变量

    $this->set('result', 'something');
    

    并在您的 travel.ctp 中将值“某物”传递给您的元素,包括:

    element("profile", array('result' => $result));?>

    在您的 profile.ctp 中,您可以正常使用此变量,例如:

    $result
    

    要将数据保存到另一个表中,您可以更改表单的名称

     <?php echo $this->Form->create('User', array('url' => array('action' => 'travel')));?>
    

    或不设置表单的名称并手动将模型加载到您要保存数据的地方

    <?php echo $this->Form->create(null, array('url' => array('action' => 'travel')));?>
    

    如果您想重用元素但更改操作,您可以传递一个设置操作的值,例如:

    在你的 travel.ctp 中

    <?php echo $this->element("profile", array('result' => $result, 'action' => 'youraction'));?>
    

    在你的 profile.ctp 中

    <?php echo $this->Form->create('User', array('url' => array('action' => $action)));?>
    

    【讨论】:

    • 你在吗?我对你的答案还有一些疑问?
    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2016-09-25
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多