Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.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
Javascript 如何在cakephp中实现ajax post提交_Javascript_Php_Jquery_Ajax_Cakephp - Fatal编程技术网

Javascript 如何在cakephp中实现ajax post提交

Javascript 如何在cakephp中实现ajax post提交,javascript,php,jquery,ajax,cakephp,Javascript,Php,Jquery,Ajax,Cakephp,我曾上网尝试用cakephp实现ajax post提交,但没有成功, 我如何实现这一功能,就像twitter上的功能一样,以及如何在有新记录时自动更新帖子? 这是我的邮政编码: public function add($id = null) { if ($this->request->is('post')) { $this->request->data['Post']['user_id'] = $this->Session->read('Auth.

我曾上网尝试用cakephp实现ajax post提交,但没有成功, 我如何实现这一功能,就像twitter上的功能一样,以及如何在有新记录时自动更新帖子? 这是我的邮政编码:

public function add($id = null) {
   if ($this->request->is('post')) {
   $this->request->data['Post']['user_id'] = $this->Session->read('Auth.User.id');
   $this->Post->create();
   if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('The post has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.'));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
这是我的邮寄表格:

<?php echo $this->Form->create('Post'); ?>
<fieldset>
<legend><?php echo __('Add Post'); ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('content');
?>
</fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>

对于Ajax post提交,请尝试此方法

将提交按钮代码替换为以下代码:

<?php echo $this->Ajax->submit('SUBMIT',
    array(
        'class'=>'btn', 
        'div'=>false, 
        'label'=>false, 
        'url' => array(
            'controller' =>'your_controller',
            'action' =>'your_function'
        ),
        'update' =>'update_section_id', 
        )
    ); 
?>

Jquery需要:

$('#yourFormId').on('submit', function(event){

  event.preventDefault();

  $.jax({
   type: "post",
   url: "formSubmit",  //your form submission route
   data: {data1: $('input[name="yourInputName"]').val(), //you can add more here},
   dataType: "json",
   cache: false,
   success: function(result){
   //Do something with returned data
}
})

})
控制器

public funtion formSubmit(){
  $this->autoRender = false;

  if($this->request->is('ajax')){
   $data = $this->request->data;
  //Do something with data
  //If save or update successful
  $response = array('status' => 'success', 'msg' => 'your msg');
  return json_encode($response);

}
}

检查此链接中的问题本身:-