【问题标题】:Unable to save data in database through ajax in CakePhp after passing my own array variable in save function?在保存函数中传递我自己的数组变量后,无法通过 CakePhp 中的 ajax 将数据保存在数据库中?
【发布时间】:2015-05-29 11:33:13
【问题描述】:

这是我的视图 ctp 页面:

<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>

这是我的 PostsController 函数

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";
               }
            }
        }
}

在 save 中使用数组作为 $inputData ,每当我点击提交按钮时,数据库中都不会保存任何内容。

但是当我在保存函数中传递 $this->data 时,id、created 和 modified 等列被填充,但 title 和 body 列留空。

我的 posts 数据库包含列 id、title、body、created、modified

【问题讨论】:

    标签: php jquery ajax cakephp


    【解决方案1】:

    你能用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);                                       
                }
    
        });
    

    您没有在 ajax 请求中发送数组,这就是为什么您没有在 $this->data 中获取数组的原因。但首先在后控制器中使用echo "&lt;pre&gt;";print_r($this-&gt;data); 运行此代码,以检查您是否正在获取数据

    【讨论】:

    • 我在控制器的$this-&gt;data 中获取数据..我已经检查过了..\
    • 你能把那个数组粘贴到这里吗?
    • Array ( [Post] =&gt; Array ( [ajaxtitle] =&gt; Title 35 [ajaxbody] =&gt; content ) )
    • 你可以尝试改变这一行 echo $this->form->create("Post",array('url'=>array('controller'=>'posts','action' =>'ajaxAdd'),'id'=>'saveForm'));然后你可以发布你的数组
    • 为什么这有什么问题?...我需要在我的posts 控制器中使用我的ajaxAdd 函数。
    【解决方案2】:

    如果您使用的是 CakePHP 3.0 而不是早期版本,您应该使用 $this->request->data() 而不是 $this->data。

    http://book.cakephp.org/3.0/en/controllers/request-response.html#request-body-data

    如果是这样的话,代码应该如下:

    public function ajaxAdd()
    {
        $this->autoRender=false;
          if($this->RequestHandler->isAjax()){
             Configure::write('debug', 0);
          }
            if(!empty($this->request->data())){
                $inputData = array();
                $inputData['Post']['title'] = $this->request->data('Post.ajaxtitle');
                $inputData['Post']['body'] = $this->request->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";
               }
            }
        }
    

    【讨论】:

    • (会使用评论来检查正在使用的 CakePHP 版本,但我没有足够的声誉来为帖子添加评论)
    • 我使用的是 CakePhp 2.6.4 版
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    相关资源
    最近更新 更多