O
O
OlegGazmanov2020-07-13 11:21:31
AJAX
OlegGazmanov, 2020-07-13 11:21:31

Why doesn't Ajax post update script work?

I wrote a small script for editing a post, in principle everything works, except for updating the picture, and I don’t understand what the reason is. Without Ajax, everything works as it should. I tried to see what formData will show me through console.log and alert, it shows all the data except for the image

$("document").ready(function() {
            $("#editPostButton{{$post->id}}").click(function() {

                var formData = $("#EditPostForm{{$post->id}}").serialize();

                $.ajax({
                    url: "{{route('editPost', ['id' => $user->id, 'postId' => $post->id])}}",
                    type: "POST",
                    data: formData,
                    success: function(data) {
                        $("#textpostdata{{$post->id}}").html($(data).find("#textpostdata{{$post->id}}").html());
                        $("#closeButton{{$post->id}}").click();
                    }
               });

            });
        });


The form itself
<form action="{{route('editPost', ['id' => $user->id, 'postId' => $post->id])}}" method="post" enctype="multipart/form-data" id="EditPostForm{{$post->id}}" name="postForm">
    @csrf @method('PATCH')
    <div class="form-group">
        <textarea maxlength="100" name="title" class="form-control" rows="1">{{$post->title}}</textarea>
    </div>
    <div class="form-group">
        <textarea id="message" maxlength="5000" name="message" class="form-control" rows="10">{{$post->message}}</textarea>
    </div>
    <div class="form-group">
        <textarea maxlength="100" class="form-control mt-1" id="videoPost" name="videoPost" cols="100" rows="1">{{$post->videoPost}}</textarea>
    </div>
    <h6>Current image</h6>
    <img src="{{$post->img}}" class="img-fluid mb-2" width="230">
        <div class="form-group">
            <input type="file" id="img" name="img" accept="image/*">
        </div>

    <button type="button" class="btn btn-sm btn-primary" id="editPostButton{{$post->id}}">Edit</button>
</form>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question