Show Single post in laravel with ajax call

Posted 1 year ago by beginner_luck

Hello can someone help me through this, I want to show the data of my single post, let say I have a list of post and there was a button on it as "VIEW", when I click the button the single post will then show and hide the table/list of post.

View Button:

                            <button class="show-post btn btn-success" data-id="{{$post->id}}" data-title="{{$post->title}}" data-slug="{{$post->slug}}" data-category="{{$post->category_id}}" data-image="{{$post->image}}" data-body="{{$post->body}}">
                                <span class="glyphicon glyphicon-eye-open"></span> View</button> 
                            <button class="edit-post btn btn-info" data-id="{{$post->id}}" data-title="{{$post->title}}" data-slug="{{$post->slug}}" data-category ="{{$post->category_id}}" data-image="{{$post->image}}" data-body="{{$post->body}}">
                                <span class="glyphicon glyphicon-edit"></span> Edit</button>
                       

                </td>

the data will show in this button, but when I click the VIEW button, it showed me a wrong data in the single post.

My Code for displaying the single post: $(document).on('click', '.show-post', function() {

        var id = $(this).attr('data-id');
        var title = $(this).attr('data-title');
        var slug = $(this).attr('data-slug');
        var category = $(this).attr('data-category');
        var featured_image = $(this).attr('data-image');
        var body = $(this).attr('data-body');
    
        $('#show-title').val(title);
        $('#show-slug').val(slug);
        $('#show-category').val(category);
        $('#show-image').val(featured_image);
        $('#show-body').val(body);
        $('#show-id').val($(this).data('id'));

        $('.show-single-post').css('display','block');
        $('.posts-table').css('display','none');
    });

Please sign in or create an account to participate in this conversation.