0

I have a simple form with a file upload input that doesn't dump the file input value.

This is the form code that i have:

<!-- Modal -->
        <div class="modal fade" id="app-form" tabindex="-1" role="dialog" aria-labelledby="app-form-label">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="app-form-label">Ședință foto boudoir!</h4>
                    </div>

                    {!! Form::open(['url'=>'','method'=>'POST', 'files'=>true, 'id' => 'model-form']) !!}
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
                                    {!! Form::label('first_name', 'Nume') !!}
                                    {!! Form::text('first_name', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('first_name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('first_name') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
                                    {!! Form::label('last_name', 'Prenume') !!}
                                    {!! Form::text('last_name', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('last_name'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('last_name') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('age') ? ' has-error' : '' }}">
                                    {!! Form::label('age', 'Vârsta') !!}
                                    {!! Form::text('age', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('age'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('age') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('sex') ? ' has-error' : '' }}">
                                    {!! Form::label('sex', 'Vârsta') !!}
                                    {!! Form::select('sex', ['M' => 'Masculin', 'F' => 'Feminin'], null, ['class' =>
                                    'form-control',
                                    'placeholder' =>
                        '-- Selectează --']) !!}
                                    @if ($errors->has('sex'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('sex') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                    {!! Form::label('email', 'Email') !!}
                                    {!! Form::email('email', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('email') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>

                            <div class="col-xs-12 col-sm-6">
                                <div class="form-group{{ $errors->has('phone') ? ' has-error' : '' }}">
                                    {!! Form::label('phone', 'Telefon') !!}
                                    {!! Form::text('phone', '', ['class' => 'form-control']) !!}
                                    @if ($errors->has('phone'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('phone') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-12">
                                <div class="form-group{{ $errors->has('details') ? ' has-error' : '' }}">
                                    {!! Form::label('details', 'Detalii') !!}
                                    {!! Form::textarea('details', '', ['class' => 'form-control', 'rows' => 10]) !!}
                                    @if ($errors->has('details'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('details') }}</strong>
                                         </span>
                                    @endif
                                </div>
                            </div>
                        </div>

                        {!! Form::label('image', 'Fotografii Reprezentative') !!}
                        {!! Form::file('image') !!}
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-warning card-2">Trimite Datele</button>
                    </div>
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
        <!-- /End Modal -->

I use ajax to do the post with this code:

<script>
    (function () {
        $('#model-form').submit(function (e) {
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: 'store',
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData:false,
                success: function (response) {
                    console.log($('#image').val());
                }
            });
        });
    })();
</script>

Now don't ask me why i set the csrf-token headers in ajax, that's another problem but for now, the problem is that is i do a $_POST & $_FILES dump after i submit the form, i have all the values of the form except the image file upload data. I'm pulling my hair out with this so please help me!

5
  • dumping $_POST for the file?? You mean $_FILES? You should dump the $_FILES superglobal for your uploaded files Commented Nov 11, 2016 at 16:10
  • Yes, sorry, i did $_FILES to. Commented Nov 11, 2016 at 16:12
  • No, no console errors. Commented Nov 11, 2016 at 16:35
  • As long as i get all the input data except the image one...what do you think? Commented Nov 11, 2016 at 16:41
  • Your code works fine for me. All I did was copy and paste your html/js into a view, and dumped Request::all() inside a closure for Route::post('store'). Can you post what you have in routes.php for Route::post('store', ... and any related controller functions if any? Commented Nov 11, 2016 at 16:50

1 Answer 1

4

You should use enctype=multipart/form-data whenever you're trying to send file data in the form like this:

<form action="" method="POST" enctype="multipart/form-data">
</form>

You can dump the inputs using laravel's dd() method inside controller like this:

dd(request()->all());

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I used dd(request)->all() and i saw the files... Why did'nt work with var_dump($_FILES) ?!
Don't know truly but, Cheers, for solving your problem :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.