abarua's avatar

Upload an image using modal

Hi, I want to upload an image using modal. I know how to to that using an HTML form, but I am not sure how to use an ajax request to send data to the controller. Thanks in advance!

The controller:

public function postCreatePost(Request $request)
    {

        ...
        $post->longitude = $request['longitude'];

        if($request->user()->posts()->save($post))
        {
            $message = 'Post Successful';
        };

        $file = $request->file('image');
        $filename = $post->id.'.jpg';
        if($file)
        {
            Storage::disk('local')->put($filename, File::get($file));
        }

        return redirect()->route('dashboard')->with(['message' => $message]);
    }

The ajax request that I have so far:

$('#modal-save').on('click',function(){
        //create ajax request
        $.ajax({
            method: 'POST',
            url: urlEdit, //url route is defined in the dashboard html script
            //pass the post content and id to body and postId which will be used in the Post controller edit method
            //need the token to avoid error
            data: {body: $('#post-content').val(), postId: postId, _token: token}
        })
            .done(function (msg){
                //console.log(msg['message']);
                //console.log(JSON.stringify(msg));
                $(postBodyElement).text(msg['new_content']);    //update the post w/o refreshing the page
                $('#edit-post').modal('hide');  //hide the modal
            })
    });

The modal:

<div class="modal" tabindex="-1" role="dialog" id="edit-post">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Edit Post</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="post-content">Edit Post</label>
                            <textarea class="form-control" name="post-content" id="post-content" rows="5"></textarea>
{{--                            copy meta data using jquery?--}}
{{--                            add a separate image form here--}}
                            <label for="image">Image (jpg only)</label>
                            <input type="file" name="image" class="form-control" id="image">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
                </div>
            </div>
        </div>
    </div>
0 likes
4 replies
hupp's avatar

try like below

<form id="giveIdHere" enctype="multipart/form-data">
@csrf

$('#modal-save').on('click',function(){
        //create ajax request
    var formData = new FormData($('#giveIdHere'))
        $.ajax({
            method: 'POST',
            url: urlEdit, //url route is defined in the dashboard html script
            //pass the post content and id to body and postId which will be used in the Post controller edit method
            //need the token to avoid error
            data: formData,
        })
            .done(function (msg){
                //console.log(msg['message']);
                //console.log(JSON.stringify(msg));
                $(postBodyElement).text(msg['new_content']);    //update the post w/o refreshing the page
                $('#edit-post').modal('hide');  //hide the modal
            })
    });
1 like
abarua's avatar

I can save all other entries in the form, but cannot upload the image. Do I have to do anything specific with the http request? I cannot see the file in the storage. Does that help to locate the error?

abarua's avatar

Tried with image: $('#image').val() but not sure if I am doing it right.

$('#modal-save').on('click',function(){
        // var formData = new FormData($('#postmaster'));
        //create ajax request
        $.ajax({
            method: 'POST',
            url: urlEdit,
            data: {image: $('#image').val(), body: $('#post-content').val(), postId: postId , _token: token}
       

        })

Please or to participate in this conversation.