我创建了一个表单,它通过Ajax提交它的数据。表单显示了成功消息,但当我看到响应时,它在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);
}
您发送名为comment
的参数,但在服务器中您正在检查comments
。修改下面一行:
$comments = $this->input->post('comment'); // < note the 's' is removed