2016-09-06 54 views
-2

我已经使用上面的代码创建了一个表单,但我提交了他们,我收到了空数据。cakephp +发布数据为空

我尝试调试通过

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(); ?> 

生成形式是:

<form accept-charset="utf-8" method="post" enctype="multipart/form-data" id="basic_form_validation" novalidate="novalidate" class="form-horizontal" action="#users/businesses/background"> 
<div style="display:none;"><input type="hidden" value="POST" name="_method"></div> 
<div class="item in"> 
    <div class="change-background"> 
     <label> 
      Change Background : 
     </label> 
     <input type="file" value="" id="background" name="background"> 
     <input type="hidden" value="20015" name="userid"> 
    </div> 
</div> 
<input type="submit" name="change" value="Change"> 

我怎样才能解决这个问题,有什么问题。

+0

通过按CTRL + U或F12来查看生成的源代码。 – Phiter

+0

@PhiterFernandes:这就是输出结果'

' –

+0

完整的表单源代码。我对cakephp不太了解,但也许表格是错误的。 – Phiter

回答

0

'URL'=> '#用户/测试/背景'

你并不需要写的URL,表单助手为你做正确

一个例子用图像形式的上传

<?= $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']); 
}