dongquxiao8545 2015-05-29 11:33
浏览 44

在保存函数中传递我自己的数组变量后,无法通过CakePhp中的ajax保存数据库中的数据?

This is my view ctp page:

<h1>Add Post</h1>
<?php echo $this->form->create(null,array('url'=>array('controller'=>'posts','action'=>'ajaxAdd'),'id'=>'saveForm'));
echo $this->form->input('ajaxtitle');
echo $this->form->input('ajaxbody',array('rows'=>'3'));
echo $this->form->end('Save Post');
?>

<script>
    $(document).ready(function(){
    $("#saveForm").submit(function(){       
        var formData = $(this).serialize();
        var formUrl = $(this).attr('action');
        $.ajax({

            type:'POST',
            url:formUrl,
            data:formData,
            success: function(data,textStatus,xhr){
                alert(data);                                       
                }

        });
        return false;
    });
});
</script>

This is my PostsController function

class PostsController extends AppController
{

        public $name = 'Posts';
        public $helpers = array('Html', 'Form', 'Session');
        public $components  = array('RequestHandler');
public function ajaxAdd()
    {
        $this->autoRender=false;
          if($this->RequestHandler->isAjax()){
             Configure::write('debug', 0);
          }
            if(!empty($this->data)){
                $inputData = array();
                $inputData['Post']['title'] = $this->data['Post']['ajaxtitle'];
                $inputData['Post']['body'] = $this->data['Post']['ajaxbody'];
                $data = $this->Post->findByTitle($inputData['Post']['title']);
                $this->Post->create();
               if(empty($data))
               {                   
                  if($this->Post->save($inputData))
                      return "success"; 
                }else
                {
                 return "error";
               }
            }
        }
}

With array as $inputData in save , whenever I click over the submit button nothing is being saved in the database.

But when I pass $this->data in save function, columns like id,created and modified are filled but the title and body column are left blank.

My posts database contains columns id,title,body,created,modified.

  • 写回答

2条回答 默认 最新

  • dsj60862 2015-05-29 20:08
    关注

    Can you try below code in javascript

    $("#saveForm").submit(function(){       
        var formData = $(this).serializeArray();
        var formUrl = $(this).attr('action');
        $.ajax({
    
            type:'POST',
            url:formUrl,
            data:formData,
            success: function(data,textStatus,xhr){
                alert(data);                                       
                }
    
        });
    

    You are not sending array in ajax request thats why you are not getting array in $this->data. but first run this code with echo "<pre>";print_r($this->data); in post controller to check whether you are getting data or not

    评论

报告相同问题?

悬赏问题

  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退