CakePHP 3动态内容

时间:2016-06-21 07:49:23

标签: jquery cakephp add

我已经构建了一个APP,我希望在一个页面上动态地添加内容。所以我将带有jQuery的输入字段添加到页面(1,5或10)并将其存储在我的数据库中。

首先我想知道这是否是最佳做法。 Seconde为什么我会收到错误

Notice (8): Undefined variable: content [APP/Template/Contents/add.ctp, line 8]

我的ContentConroller添加功能

public function add()
{
    $content = $this->Contents->newEntity();
    $id = $this->Auth->user('id');

    if ($this->request->is('post')) {
        $contents = $this->Contents->newEntities($this->request->data());
        foreach ($contents as $content) {
            $content->article_id = $id;
            $this->Contents->save($content);
        }
            $this->Flash->success(__('Added'));
            return $this->redirect(['controller'=> 'articles', 'action' => 'index']);
        }




    $this->set(compact('content'));
    $this->set('_serialize', ['content']);
}

我的内容add.ctp(到目前为止没有动态加载输入)

<nav class="large-3 medium-4 columns" id="actions-sidebar">
    <ul class="side-nav">
        <li class="heading"><?= __('Actions') ?></li>
        <li><?= $this->Html->link(__('List Contents'), ['action' => 'index']) ?></li>
    </ul>
</nav>
<div class="contents form large-9 medium-8 columns content">
    <?= $this->Form->create($content) ?>
    <fieldset>
        <legend><?= __('Add Content') ?></legend>
        <?php
            echo $this->Form->input('0.article_id');
        echo $this->Form->input('1.article_id');
            echo $this->Form->input('0.type');
            echo $this->Form->input('0.position');
        echo $this->Form->input('1.type');
        echo $this->Form->input('1.position');
            echo $this->Form->input('0.text');
            echo $this->Form->input('1.text');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>

</div>
编辑:修正了它。但仍然。最佳实践?

0 个答案:

没有答案