我试图对帖子发表评论,因为我已经检索了所有帖子。
我有一个名为评论的按钮,当我单击该按钮时,我想获取当前评论body
,post_id
和user_id
,以便将其保存在comments
表。
这是问题:
当我点击第一篇帖子的评论时,评论body
,post_id
和user_id
的显示效果非常好,我已经在 ajax 请求之前通知了他们,可以在第一篇文章中发布,但是对于剩下的posts
,当我单击评论按钮时,我会收到{{1} }(第一篇帖子)中的任何内容,而id
则无任何评论,即使我单击除第一篇帖子以外的评论按钮,它也会返回body
的第一篇文章。
我正在使用Laravel,这是我的代码:
控制器
id
Route::post('comment/{post_id}',[
'uses' => 'CommentController@storeComments',
]);
功能
storeComments
这是我的表格。
我使用了 public function storeComments(Request $request,Comment $body,$post_id){
if(!$request->ajax()){
$comment = new Comment;
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id;
$comment->body = Input::get('body');
$comment->save();
$response = array(
'status' => 'success',
'msg' => 'Setting created successfully',
);
return Response::json($response);
return 'yes';
}else{
return 'no';
}
}
循环来获取所有帖子,并且在该循环中,我使用了foreach
来以下面的形式发表评论。
textarea
这是 Ajax请求
@foreach
<form action="{{ url('comment',$post->p_id) }}" method="POST" >
@csrf
<textarea
name="body"
placeholder="Write A Suggestion"
data-autosize-input='{ "space": 100 }'
rows="50" cols="100"
name="comment"
class="form-control comment body">
</textarea>
<input type="hidden" value="{{csrf_token()}}">
<!-- user Id of the logged In one -->
<input type="text" class="user_id" value="{{Auth::user()->id}}" name="user_id">
<!-- post id -->
<input type="text" class="post_id" name="post_id" value="{{$post->p_id}}">
<button type="button" class="btn btn-sm btn-danger formcomment" value = 'Comment' style="margin:10px;">Comment</button>
</form>
@endforeach
注意:
我已经搜索了这个问题,有人建议将<!-- actuall here control does not comes okay -->
<script type="text/javascript">
$(document).ready(function(){
$('.formcomment').on('click', function(e) {
e.preventDefault();
// these are id's
var body = $('.body').val();
var post_id = parseInt($('.post_id').val());
var user_id = parseInt($('.user_id').val());
alert(body);
alert('this is post'+post_id);
alert('This is user id'+user_id);
$.ajax({
type: "POST",
url: '/comment',
data: {body:body, post_id:post_id, user_id:user_id},
success: function(msg) {
$("body").append("<div>"+msg+"</div>");
}
});
});
});
更改为id
,因为在Jquery中,class
对于不同的字段应该是唯一的。
由于表单位于id's
循环内,因此每个帖子的字段都不同。
希望这有道理,我真的尽了最大努力使它变得简单。
我是laravel的新手
答案 0 :(得分:0)
您的帖子中的每个帖子都包含.body,.post_id和.user_id这3个元素。单击按钮时,将基于此类选择输入字段。但是,由于您具有该类的多个元素,因此始终只选择第一个会导致错误的元素。您需要从单击链接的表单中选择类:
更改
// these are id's
var body = $('.body').val();
var post_id = parseInt($('.post_id').val());
var user_id = parseInt($('.user_id').val());
到
var form = $(this).closest('form);
// these are id's
var body = form.find('.body').val();
var post_id = parseInt(form.find('.post_id').val());
var user_id = parseInt(form.find('.user_id').val());
这应该为您提供正确格式的元素
答案 1 :(得分:0)
尝试
Route::post('comment/{post_id}',[
'uses' => 'CommentController@storeComments',
]);
storeComments Function
public function storeComments(Request $request,$post_id){
if(!$request->ajax()){
$comment = new Comment;
$comment->user_id = Auth::user()->id;
$comment->post_id = $post_id;
$comment->body = $request->get('body');
$comment->save();
$response = array(
'status' => 'success',
'msg' => 'Setting created successfully',
);
return Response::json($response);
return 'yes';
}else{
return 'no';
}
}
Here is my form. I've used foreach loop to get all the post and there inside that loop I've used textarea for the comment in the following form.
@foreach
<form method="POST" >
@csrf
<textarea
name="body" id="body"
placeholder="Write A Suggestion"
data-autosize-input='{ "space": 100 }'
rows="50" cols="100"
name="comment"
class="form-control comment body">
</textarea>
<input type="hidden" value="{{csrf_token()}}">
<!-- user Id of the logged In one -->
<input type="text" class="user_id" value="{{Auth::user()->id}}" name="user_id">
<!-- post id -->
<input type="text" class="post_id" name="post_id" value="{{$post->p_id}}">
<button type="button" class="btn btn-sm btn-danger formcomment" value = 'Comment' style="margin:10px;">Comment</button>
</form>
@endforeach
Here is Ajax Request
<!-- actuall here control does not comes okay -->
<script type="text/javascript">
$(document).ready(function(){
$('.formcomment').on('click', function(e) {
e.preventDefault();
// these are id's
var body = $('#body').val();
var post_id = parseInt($('#post_id').val());
var user_id = parseInt($('#user_id').val());
alert(body);
alert('this is post'+post_id);
alert('This is user id'+user_id);
$.ajax({
type: "POST",
url: "/comment/{{$post->p_id}}",
data: {body:body, post_id:post_id, user_id:user_id},
success: function(msg) {
$("body").append("<div>"+msg+"</div>");
}
});
});
});