我不想使用CakePHP的FormHelper,因为我想在我的应用程序中使用一些Ajax。如何将数据从表单传递到控制器?我$.post
从jQuery使用,但总是收到错误消息。谢谢!
您可以将Ajax与CakePHP Form Helper一起使用。
在您的.ctp文件中,输入:
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
请注意,在表单中->创建传递默认值=> false的值,该值指示表单不执行常规的“提交”。
在视图文件.ctp的底部放置:
$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哪个PHP。它基本上会获取正在提交的表单数据,并对其进行序列化,然后通过ajax将其传递给/ yourcontroller / youraction。update =>#flash告诉Cake在操作完成后更新#flash div。
记得在您的控制器中公开
public $helpers = array('Js');
public $components = array('RequestHandler');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句