cakephp 3.0将数据传递给form.php

时间:2015-04-25 13:10:16

标签: cakephp

我使用无模型表单发送联系电子邮件。我正在使用食谱中的例子。

随着对象形式数组中数据的更改,我不确定将数据传递给ContactForm.php的形式是什么

我尝试了$data['name']$data[0]['name']

查看没有数据的输入名称[]

<input type="text" class="form-control" name="name" placeholder="Your Name" required="" aria-required="true" aria-invalid="false">

PagesController.php

public function contact(){

    $this->request->allowMethod('ajax');

    $contact = new ContactForm();
    if ($this->request->is('post')) {
        if ($contact->execute($this->request->data)) {
            $data = array(
                'status' => 'successful'
            );
        } else {
            $data = array(
                'status' => 'error'
            );
        }
        $this->set(compact('data')); // Pass $data to the view
        $this->set('_serialize', 'data');
    }

}

ContactForm.php

protected function _execute(array $data)
{
    // Send an email.
    $email = new Email('default');
    $email->emailFormat('html');
    $email->template('demoTemplate')->viewVars( array('userName' => $data[0]['name'],
        'userCompany' => '$data',
        'userEmail' => '$data',
        'userPhone' => '$data'));

    $email->from('info@domain.com');
    $email->to('$data');
    $email->bcc('$data');
    $email->subject('Contact Form Submission');
    if ($email->send()) {
        return true;
    }
    else {
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

为此,我首先使用此工具advanced-rest-client,并在cakephp的代码中使用try和catch,您也可以使用debug(),示例debug($data);结合使用advanced-rest-client你可以看到结果。

如果debug不能串行化数据

$this->set('data', $data);
$this->set('_serialize', ['data']);

但即使序列化数据使用advanced-rest-client,也只需选择post选项,然后输入您想要获取post的网址。

<强>这样:

public function contact(){

  $this->request->allowMethod('ajax');

  $contact = new ContactForm();
  if ($this->request->is('post')) {
    if ($contact->execute($this->request->data)) {
        debug($this->request->data());//first 
        $datos = $this->request->data();//second 
        $data = [
          'content' => $datos,
          'status' => 'successful',
        ];
    } else {
        $datos = $this->request->data();
        $data = [
          'content' => $datos,
          'status' => 'error,
        ];
    }
    $this->set(compact('data')); // Pass $data to the view
    $this->set('_serialize', 'data');
  }
}