从控制器输出到HTML表单

时间:2019-05-07 21:16:26

标签: javascript php jquery html laravel

有人可以告诉我如何将控制器中的数据输出到HTML表单。如果用户以前已经单击链接,我想将锚的标签从“喜欢”更改为“喜欢”。

这是HTML。

<section class="row posts">
    <div class="col-md-6 col-md-3-offset">
        <header><h3>other posts</h3></header>
        @foreach($posts as $post)
            <article class="post">
                <p>{{ $post->content }}</p>
                <div class="info">Posted by {{ $post->user->username }} on {{ $post->created_at }}</div>
                <div class="interaction">
                    <a href="#" class="like" data-postid="{{ $post->id }}">Like</a>
                    @if(auth()->user() === $post->user)
                        |
                        <a href="#" class="edit" data-postid="{{ $post->id }}">Edit</a> |
                        <a href="{{ route('post.delete',['post_id' => $post->id]) }}">Delete</a>
                    @endif
                </div>
            </article>
        @endforeach
    </div>
    <script>
        var token = '{{ session()->token() }}';
        var urlLike = '{{ route('like') }}';
    </script>
</section>

从表格中获取postid的JavaScript:

...
$('.like').on('click', function (event) {
        event.preventDefault();
        postId = event.target.dataset.postid;
        var isLike = event.target.previousElementSibling==null ? true:false;
        $.ajax({
           method: 'POST',
           url: urlLike,
           data: {isLike: isLike, postId: postId, _token: token}
        })
        .done(function () {
            //change the page
        })
    })
...

路线:

Route::post('/like',[
        'uses' => 'PostController@postLikePost',
        'as' => 'like'
    ]);

最后,有人可以告诉我如何将控制器的输出发送到HTML表单吗?

public function postLikePost(Request $request)
{
    $post_id = $request['postId'];
    $is_like = $request['isLike'] === 'true' ? true : false;
    $post = Post::find($post_id);
    if (!$post) {
        return null;
    }
    $user = Auth::user();
    $like = $user->likes()->where('post_id', $post_id)->first();

    if ($like) { // user already liked the post
        $like->delete();

        return null; // output to "Like" in the html form here
    }

    $like = new Like();
    $like->post_id = $post->id;
    $like->user_id = $user->id;
    $like->save(); // output to "Liked" in the html from here

    return null;
}

如果用户已经喜欢该帖子,则“喜欢”锚点的标签应从“喜欢”更改为“喜欢”。

2 个答案:

答案 0 :(得分:0)

我将两者都设置为POST,然后在您的success块中查询结果并将其设置为likeliked。像这样:

success: function (data) {
    document.getElementById("something").innerHTML = "Liked";
}

答案 1 :(得分:0)

您可以执行以下操作,但是可以根据需要进行更改。

public function postLikePost(Request $request)
{
  $post = Post::where('id', $request->get('post_id'))->first();
  if(!$post){
    return response()->json(['status' => 1, 'message' => 'post not found']);
  }
  if($post->liked == 1)//change it as per your model
  { 
    return response()->json(['status' => 2, 'message' => 'already liked']);
  }
  $post->status = 1;
  $post->save();
  return response()->json(['status' => 3, 'message' => 'liked']);
}

以及您的ajax成功

success: function(response){
  if(response.status == 1){
   window.alert('Post Not Found')
  }
  else{
   document.querySelector('#likeBtn').innerHTML = 'liked'
  }

顶按钮

<a href="#" class="like" id="likeBtn" data-postid="{{ $post->id }}">Like</a>