我有一个工作评论表单$post->id
通过ajax提交数据,请注意没有任何表单标记。
<div class="comment-fields">
<div class="row commenter-comment">
<div class="form-group col-md-12">
<textarea id="commenter_comment" name="commenter_comment" class="form-control comment-field" title="User's comment" placeholder="Comment Text"></textarea>
</div>
</div>
<div class="row commenter-name-email">
<input type="hidden" id="commenter_parent" name="commenter_parent" class="commenter-parent" value="0">
<input type="hidden" id="commenter_post" name="commenter_post" class="commenter-post" value="{{ $post->id }}">
</div>
<div class="row commenter-captcha">
<div class="col-md-3">
<a href="javascript:void(0)" class="btn btn-success post-this-comment">Comment</a>
</div>
</div>
</div>
这是javascript处理程序
$(document).on('click', 'a.post-this-comment', function(){
var form_data = {
'per_page': $('.comments_per_page').val(),
'commenter_parent': $('#commenter_parent').val(),
'commenter_post': $('#commenter_post').val(),
'commenter_comment': $('#commenter_comment').val(),
};
var arr = [
'commenter_parent',
'commenter_post',
'commenter_comment'
];
for (var i in arr, i < arr.length, i++) {
var elem = arr[i];
form_data[elem] = $('#' + elem).val();
}
// console.log(form_data); // something like => Object {per_page: "some_value", commenter_parent: "some_value", commenter_user_id: "some_value", commenter_comment: "some_value"}
var request = $.ajax({
type: 'POST',
url: 'post_this_comment',
data: form_data,
dataType: 'json'
});
request.done(comment_done_handler);
request.fail(comment_fail_handler);
});
我想要做的就是获取当前帖子的帖子ID,这样我就可以告诉comment_list()只能获得该帖子的评论。
我甚至无法从commenter_post
方法获取comment_list()
的值,我得到null。但我能够很好地从其他方法中检索这些值。
所以我添加了一个隐藏字段(没有表单标签)到检索post_id
<input type="hidden" id="post_id" name="post_id" class="post-id" value="{{ $post->id }}">
但是,当我尝试获取该字段的值时,我总是得到null
Comment
模型
public static function root_comments($postId) { // setting $postId
return self::child_comments(0, 'desc')->where('post_id', $postId);
}
CommentController
protected function comment_list($per_page, Request $request) {
$post = Input::get('post_id');
dd($post); // returns null
$root_comments = Comment::root_comments(1); // I am setting the postId manually here
$root_with_replies = $this->include_replies_for($root_comments);
$paginated_comments = $this->paginate($root_with_replies, $per_page, $request);
return $paginated_comments;
}
public function index(Request $request) {
$view_data = self::view_data($request);
return view('eastgate.comment.leave_a_comment', $view_data);
}
答案 0 :(得分:1)
尝试转储整个请求对象并查看POST或GET请求中的参数,确保您的字段在那里,如果没有表单可能出错的话。
隐藏字段是否在调用comments_list函数之前提交的实际表单内?
答案 1 :(得分:0)
您已为此问题标记了Laravel-5,但正在使用Laravel 4 method of retrieving input。对于Laravel 5 input retrieval,请尝试:
$post = $request->input('post_id');
此外,根据Controller help page,您的路由参数应该在您的其他依赖项之后。
CommentController
protected function comment_list(Request $request, $per_page) {
$post = $request->input('post_id');
dd($post);
$root_comments = Comment::root_comments(1);
$root_with_replies = $this->include_replies_for($root_comments);
$paginated_comments = $this->paginate($root_with_replies, $per_page, $request);
return $paginated_comments;
}