【问题标题】:comments value is working for first post ONLY评论值仅适用于第一篇文章
【发布时间】:2019-06-13 11:46:16
【问题描述】:

我正在尝试对帖子发表评论,因为我已经检索了所有帖子。

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

这是问题: 当我为 first 帖子点击 Comment 时,bodypost_iduser_id 的评论被提取得非常好,我之前已经提醒过他们 ajax 请求,第一次发帖没问题,但是当我点击 Comment 按钮时,对于剩下的 posts,我得到了 firstid > 发布并没有评论body,即使我点击评论按钮而不是第一篇文章,它也会返回第一篇文章的id

我正在使用 Laravel,这是我的代码:

控制器

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

这里是 Ajax 请求

<!--  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 循环内,并且每个帖子的字段都不同。

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

我是 laravel 新手

【问题讨论】:

    标签: javascript php jquery laravel


    【解决方案1】:

    您的每一篇文章都有 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());
    

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

    【讨论】:

    • 太棒了,真的很有帮助
    • 好吧,我正确地获取了这些值,但它们没有被发送到我使用过的url,即评论。当我单击评论按钮时,在控制台中显示page not found。你能看看顶部的route 和ajax 请求中的url,它们还好吗? @Lapskaus
    • 我对 Laravel 不熟悉,但是您发布的路线似乎期望在 url 中有一个 id,您只需在 ajax 请求中使用 /comment 调用它,尽管将 post_id 添加到您的 url:url : '/comment' + post_id,
    • 是的,我尝试通过将post_id 作为参数传递,但这不起作用!
    【解决方案2】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 2021-08-04
      • 1970-01-01
      相关资源
      最近更新 更多