cakephp + post数据为null

时间:2016-09-06 11:06:35

标签: php cakephp post

我使用上面的代码创建了一个表单但我提交了它们我得到了空数据。

我尝试通过

进行调试
class Dog {

    private String name;

    public Dog(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        System.out.println("inside equals");
        if ((obj instanceof Dog) && this.name.equals(((Dog) obj).name)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
}

但数据为空。

debug($this->data); //exit;
pr($_FILES); 
pr($this->request->data); die;

生成的表格是:

<?php
echo $this->Form->create(false, array(
    'class' => 'form-horizontal',
    'type' => 'file',
    'novalidate' => true,
    'url' => '#users/test/background',
    'id' => 'basic_form_validation',
));
?>

<div class="item in">
    <div class="change-background">
        <label>
            Change Background :
        </label>
        <input type="file" name="background" id="background" value="" />
        <input type="hidden" name="userid" value="<?= $user['id']; ?>" />
    </div>
</div> 
<input type="submit" value="Change" name="change" />
<?php echo $this->Form->end(); ?>

如何解决此问题以及问题所在。

1 个答案:

答案 0 :(得分:0)

  

&#39; URL&#39; =&GT; &#39;#个用户/测试/背景&#39;

您不需要编写网址,表单帮助程序会为您执行此操作并正确

带图片上传的表单的一个示例

<?= $this->Form->create($category, ['role' => 'form', 'enctype' => 'multipart/form-data']) ?>
        <?php
        echo $this->Form->input('title', ['label' => 'title', 'placeholder' => __('title')]);
        echo $this->Form->file('img', ['label' => 'icon']);

        ?>
        <div class="form-group">
            <?= $this->Form->button(__('Add'), ['class' => 'btn btn-default']) ?>
        </div>
        <?= $this->Form->end() ?>

和控制器

public function add()
{
    $this -> set('title', 'Add category');
    $category = $this->Categories->newEntity();
    if ($this->request->is('post')) {
        $category = $this->Categories->patchEntity($category, $this->request->data);
        if ($this->Categories->save($category)) {
            $this->Upload->send($category->img, $category->id);
            $this->Flash->success(__('Data saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('Couldn\'t save data'));
        }
    }
    $this->set(compact('category'));
    $this->set('_serialize', ['category']);
}