提交表单时,在JqueryAjax响应中获取Null

时间:2015-08-04 10:06:19

标签: php jquery ajax forms codeigniter

我创建了一个表单,通过Ajax提交数据。表单显示了成功消息,但是当我看到Response时,它在1个数据字段中显示Null。

发表:

teacher_id    6
user_id    4
comment   MyCommentttttt

响应:

{"teacher_id":"6","user_id":"4","comment":null}

我写了以下代码,它成功发送了teacher_id& user_id但无法发送comment数据。请检查代码并指导我在这里做错了什么。

查看-Jquery

var teacherId= $("input#teacher_id").val();
    var userId= $("input#user_id").val();
    var commentText =$("textarea#comments").val() ; 

    jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>index.php/data_controller/user_comments",
            dataType: "json", 
            data: {teacher_id:teacherId, user_id: userId, comment: commentText },
            success: function(){
                    alert("success");
                },
            error: function()   {
                alert("Fail");
                }

    });

控制器

public function user_comments()
    {
        $teacher_id= $this->input->post('teacher_id');
        $user_id= $this->input->post('user_id');
        $comments= $this->input->post('comments');

        $data= array(
                    'teacher_id'    =>  $teacher_id,
                    'user_id'       =>  $user_id,
                    'comment'       =>  $comments,
                    'Date'          =>  date('Y-m-d')   
        );

    //  $this->load->model('comments');
    //  $this->comments->add_comments($data);

        echo json_encode($data);

    }   

1 个答案:

答案 0 :(得分:1)

您发送名为comment的参数,但在服务器中您需要检查comments。更改以下行:

$comments = $this->input->post('comment'); // < note the 's' is removed