Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP中的HTML表单_Php_Jquery_Html_Cakephp_Formhelper - Fatal编程技术网

CakePHP中的HTML表单

CakePHP中的HTML表单,php,jquery,html,cakephp,formhelper,Php,Jquery,Html,Cakephp,Formhelper,我不想使用CakePHP中的FormHelper,因为我想在我的应用程序中使用一些Ajax。 如何将数据从表单传递到控制器?我正在使用jQuery中的$.post,但我总是收到一个错误。 谢谢 --jQuery-- 您可以将Ajax与CakePHP表单助手一起使用 在您的视图文件中。ctp put: echo $this->Form->create('Model', array('id'=>'YourFormId', array('default'=>false)));

我不想使用CakePHP中的FormHelper,因为我想在我的应用程序中使用一些Ajax。 如何将数据从表单传递到控制器?我正在使用jQuery中的
$.post
,但我总是收到一个错误。 谢谢

--jQuery--


您可以将Ajax与CakePHP表单助手一起使用

在您的视图文件中。ctp put:

echo $this->Form->create('Model', array('id'=>'YourFormId', array('default'=>false)));
echo $this->Form->input('field');
echo $this->Form->submit('Save');
echo $this->Form-->end
$data = $this->Js->get('#YourFormId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#YourFormId')->event(
  'submit',
   $this->Js->request(
      array('action' => 'yourAction', 'controller' => 'yourController'),
      array(
        'update' => '#flash',
        'data' => $data,
        'async' => true,    
        'dataExpression'=>true,
        'method' => 'POST'
        )
      )
     );
    echo $this->Js->writeBuffer(); 
注意,在您的表单->创建一个默认值=>false,它告诉表单不要执行正常的“提交”

在视图文件的底部。ctp put:

echo $this->Form->create('Model', array('id'=>'YourFormId', array('default'=>false)));
echo $this->Form->input('field');
echo $this->Form->submit('Save');
echo $this->Form-->end
$data = $this->Js->get('#YourFormId')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#YourFormId')->event(
  'submit',
   $this->Js->request(
      array('action' => 'yourAction', 'controller' => 'yourController'),
      array(
        'update' => '#flash',
        'data' => $data,
        'async' => true,    
        'dataExpression'=>true,
        'method' => 'POST'
        )
      )
     );
    echo $this->Js->writeBuffer(); 
以上是CakePHP JS帮助程序,帮助您编写Ajax和Javascript。它基本上获取正在提交的表单数据并将其序列化,然后通过ajax将其传递给/yourcontroller/youraction。update=>#flash告诉Cake在操作完成后更新#flash div

记住在控制器中要有公共的

public $helpers = array('Js');
public $components = array('RequestHandler'); 

您会遇到什么错误?在控制器中,您应该能够使用$\u POST,确保您的ajax/POST请求指定了要使用的字段。需要注意的一点是,如果手动创建的表单中存在标记错误或任何轻微异常,Cake通常会销毁所有post数据,并且您将面临一个奇怪而令人困惑的问题:)如果是这种情况,请查看关于如何处理“黑洞”的手册:
我不想使用CakePHP的FormHelper,因为我想在我的应用程序中使用一些Ajax
——FormHelper对您的Ajax调用做了什么?我一直使用FormHelper和Ajax,没有任何问题