评论值仅适用于第一篇文章



>我试图能够在帖子上发表评论,因为我已经检索了所有帖子。

我有一个名为"注释">的按钮,当我单击该按钮时,我想获取当前评论bodypost_iduser_id,以便将它们保存在comments表中。

这是问题所在: 当我单击第一篇文章的评论时,评论bodypost_iduser_id都得到了很好的获取,我已经在ajax请求之前提醒了他们,他们对于第一篇文章是可以的,但是posts当我点击评论按钮时,我得到了第一篇文章id,没有任何评论body, 即使我单击第一篇文章以外的评论按钮,它也返回第一篇文章的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进行评论。

@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更改为class,因为在 Jquery 中id's对于不同的字段应该是唯一的。

由于表单位于循环@foreach并且每个帖子的字段都不同。

希望这有点道理,我真的尽力让它变得简单。

我是拉拉维尔的新手

你的每篇帖子都有 3 个元素 .body, .post_id 和 .user_id.单击按钮时,可以根据此类选择输入字段。但是由于您具有该类的多个元素,因此您始终只选择第一个会导致错误的元素。您需要从单击链接的表单中选择类:

改变

// 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());

这应该为您提供正确形式的元素

试试这个

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

});