Laravel Upload FIle with AJAX not working

Posted 3 years ago by MarkMatute

I tried submitting the form with non ajax request and it works, but in an ajax requested form the file was not inside the request heres my form

{!! Form::open(['id'=>'saveEmployeeForm','files'=>true]) !!}
                            <div class="box-body">
                            <h4>Account Details</h4>
                            <div class="row">
                                <div class="col-lg-4 col-md-4">
                                    <img src="{{asset('images/employees/default.jpg')}}" class="employee-profile-image img-responsive img-thumbnail" id="previewHolder">
                                    <!--File Photo-->
                                    <div class="form-group">
                                        <label for="filePhoto">File Photo:</label>
                                         {!! Form::file('filePhoto',['class'=>'form-control borrowerImageFile','id'=>'filePhoto']) !!}
                                    </div>
                                    <!-- END OF File Photo-->
                                </div>
                                <div class="col-lg-8 col-md-8">
                                    <!--Username-->
                                    <div class="form-group col-lg-12">
                                        <label for="username">Username:</label>
                                        {!! Form::text('username',null,['class'=>'form-control','id'=>'username']) !!}
                                    </div>
                                    <!-- END OF Username-->
                                    <!--Password-->
                                    <div class="form-group col-lg-12">
                                        <label for="password">Password:</label>
                                        {!! Form::input('password','password',null,['class'=>'form-control','id'=>'password']) !!}
                                    </div>
                                    <!-- END OF Password-->
                                    <!--Confirm Password-->
                                    <div class="form-group col-lg-12">
                                        <label for="password_confirmation">Confirm Password:</label>
                                        {!! Form::input('password','password_confirmation',null,['class'=>'form-control','id'=>'password_confirmation']) !!}
                                    </div>
                                    <!-- END OF Confirm Password-->
                                </div>
                            </div>
                            <hr/>
                            <h4>Personal Details</h4>
                                {{--//NAMES--}}
                                <div class="row">
                                     <!--First Name-->
                                     <div class="form-group col-lg-6">
                                         <label for="firstname">First Name:</label>
                                         {!! Form::text('firstname',null,['class'=>'form-control','id'=>'firstname']) !!}
                                     </div>
                                     <!-- END OF First Name-->
                                     <!--Last Name-->
                                     <div class="form-group col-lg-6">
                                         <label for="lastname">Last Name:</label>
                                         {!! Form::text('lastname',null,['class'=>'form-control','id'=>'lastname']) !!}
                                     </div>
                                     <!-- END OF Last Name-->
                                 </div>
                                  {{--//CONTACTS--}}
                                  <div class="row">
                                        <!--Phone Number-->
                                        <div class="form-group col-lg-6">
                                            <label for="phonenumber">Phone Number:</label>
                                            {!! Form::text('phonenumber',null,['class'=>'form-control','id'=>'phonenumber']) !!}
                                        </div>
                                        <!-- END OF Phone Number-->
                                        <!--Emai-->
                                        <div class="form-group col-lg-6">
                                            <label for="email">Email:</label>
                                            {!! Form::text('email',null,['class'=>'form-control','id'=>'email']) !!}
                                        </div>
                                        <!-- END OF Emai-->
                                  </div>
                                  {{--//ADDRESS LINE--}}
                                  <div class="row">
                                        <!--Address 1-->
                                        <div class="form-group col-lg-6">
                                            <label for="address1">Address 1:</label>
                                            {!! Form::text('address_1',null,['class'=>'form-control']) !!}
                                        </div>
                                        <!-- END OF Address 1-->
                                        <!--Address 2-->
                                        <div class="form-group col-lg-6">
                                            <label for="address2">Address 2:</label>
                                            {!! Form::text('address_2',null,['class'=>'form-control']) !!}
                                        </div>
                                        <!-- END OF Address 2-->
                                  </div>

                                  {{--CITY / STATE--}}
                                  <div class="row">
                                       <!--City-->
                                       <div class="form-group col-lg-6">
                                           <label for="city">City:</label>
                                           {!! Form::text('city',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF City-->

                                       <!--State/Province-->
                                       <div class="form-group col-lg-6">
                                           <label for="province">State/Province:</label>
                                           {!! Form::text('province',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF State/Province-->
                                  </div>

                                  {{--ZIP / COUNTRY--}}
                                  <div class="row">
                                       <!--Zip-->
                                       <div class="form-group col-lg-6">
                                           <label for="zip">Zip:</label>
                                           {!! Form::text('zip',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF Zip-->
                                       <!--country-->
                                       <div class="form-group col-lg-6">
                                           <label for="country">Country:</label>
                                           {!! Form::text('country',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF country-->
                                  </div>

                                  <div class="row">
                                       <div class="form-group col-lg-12">
                                            <label for="comment">Comment:</label>
                                            {!! Form::textarea('comments',null,['class'=>'form-control']) !!}
                                       </div>
                                  </div>

                                 <div class="box-footer">
                                     <div class="col-lg-3">
                                          {!! Form::submit('Save Employee',['class'=>'btn btn-success']) !!}
                                     </div>
                                     @include('admin.partials.loading')
                                 </div>
                            </div>
                {!! Form::open(['id'=>'saveEmployeeForm','files'=>true]) !!}
                            <div class="box-body">
                            <h4>Account Details</h4>
                            <div class="row">
                                <div class="col-lg-4 col-md-4">
                                    <img src="{{asset('images/employees/default.jpg')}}" class="employee-profile-image img-responsive img-thumbnail" id="previewHolder">
                                    <!--File Photo-->
                                    <div class="form-group">
                                        <label for="filePhoto">File Photo:</label>
                                         {!! Form::file('filePhoto',['class'=>'form-control borrowerImageFile','id'=>'filePhoto']) !!}
                                    </div>
                                    <!-- END OF File Photo-->
                                </div>
                                <div class="col-lg-8 col-md-8">
                                    <!--Username-->
                                    <div class="form-group col-lg-12">
                                        <label for="username">Username:</label>
                                        {!! Form::text('username',null,['class'=>'form-control','id'=>'username']) !!}
                                    </div>
                                    <!-- END OF Username-->
                                    <!--Password-->
                                    <div class="form-group col-lg-12">
                                        <label for="password">Password:</label>
                                        {!! Form::input('password','password',null,['class'=>'form-control','id'=>'password']) !!}
                                    </div>
                                    <!-- END OF Password-->
                                    <!--Confirm Password-->
                                    <div class="form-group col-lg-12">
                                        <label for="password_confirmation">Confirm Password:</label>
                                        {!! Form::input('password','password_confirmation',null,['class'=>'form-control','id'=>'password_confirmation']) !!}
                                    </div>
                                    <!-- END OF Confirm Password-->
                                </div>
                            </div>
                            <hr/>
                            <h4>Personal Details</h4>
                                {{--//NAMES--}}
                                <div class="row">
                                     <!--First Name-->
                                     <div class="form-group col-lg-6">
                                         <label for="firstname">First Name:</label>
                                         {!! Form::text('firstname',null,['class'=>'form-control','id'=>'firstname']) !!}
                                     </div>
                                     <!-- END OF First Name-->
                                     <!--Last Name-->
                                     <div class="form-group col-lg-6">
                                         <label for="lastname">Last Name:</label>
                                         {!! Form::text('lastname',null,['class'=>'form-control','id'=>'lastname']) !!}
                                     </div>
                                     <!-- END OF Last Name-->
                                 </div>
                                  {{--//CONTACTS--}}
                                  <div class="row">
                                        <!--Phone Number-->
                                        <div class="form-group col-lg-6">
                                            <label for="phonenumber">Phone Number:</label>
                                            {!! Form::text('phonenumber',null,['class'=>'form-control','id'=>'phonenumber']) !!}
                                        </div>
                                        <!-- END OF Phone Number-->
                                        <!--Emai-->
                                        <div class="form-group col-lg-6">
                                            <label for="email">Email:</label>
                                            {!! Form::text('email',null,['class'=>'form-control','id'=>'email']) !!}
                                        </div>
                                        <!-- END OF Emai-->
                                  </div>
                                  {{--//ADDRESS LINE--}}
                                  <div class="row">
                                        <!--Address 1-->
                                        <div class="form-group col-lg-6">
                                            <label for="address1">Address 1:</label>
                                            {!! Form::text('address_1',null,['class'=>'form-control']) !!}
                                        </div>
                                        <!-- END OF Address 1-->
                                        <!--Address 2-->
                                        <div class="form-group col-lg-6">
                                            <label for="address2">Address 2:</label>
                                            {!! Form::text('address_2',null,['class'=>'form-control']) !!}
                                        </div>
                                        <!-- END OF Address 2-->
                                  </div>

                                  {{--CITY / STATE--}}
                                  <div class="row">
                                       <!--City-->
                                       <div class="form-group col-lg-6">
                                           <label for="city">City:</label>
                                           {!! Form::text('city',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF City-->

                                       <!--State/Province-->
                                       <div class="form-group col-lg-6">
                                           <label for="province">State/Province:</label>
                                           {!! Form::text('province',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF State/Province-->
                                  </div>

                                  {{--ZIP / COUNTRY--}}
                                  <div class="row">
                                       <!--Zip-->
                                       <div class="form-group col-lg-6">
                                           <label for="zip">Zip:</label>
                                           {!! Form::text('zip',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF Zip-->
                                       <!--country-->
                                       <div class="form-group col-lg-6">
                                           <label for="country">Country:</label>
                                           {!! Form::text('country',null,['class'=>'form-control']) !!}
                                       </div>
                                       <!-- END OF country-->
                                  </div>

                                  <div class="row">
                                       <div class="form-group col-lg-12">
                                            <label for="comment">Comment:</label>
                                            {!! Form::textarea('comments',null,['class'=>'form-control']) !!}
                                       </div>
                                  </div>

                                 <div class="box-footer">
                                     <div class="col-lg-3">
                                          {!! Form::submit('Save Employee',['class'=>'btn btn-success']) !!}
                                     </div>
                                     @include('admin.partials.loading')
                                 </div>
                            </div>
                       {{--</form>--}}
                       {!! Form::close() !!}

and my controller

  public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            "firstname"=>"required",
            "lastname"=>"required",
            "username"=>"required|unique:users",
            "password"=>"required|min:8|confirmed",
            "phonenumber"=>"required",
            "email"=>"required|unique:employees",
            "filePhoto"=>"required"
        ]);

        if($validator->fails()){
            return response(['valid'=>false,'errors'=>$validator->errors()]);
        }

        $userdata = $request->all();
        $userdata = array_add($userdata,'permission_id','1');
        $acode = str_replace('/',"*",bcrypt($request->input('email')));
        $userdata = array_add($userdata,'code',$acode);

        Employee::create($request->all())->user()->save(new User($userdata));
        $employee = new Employee($request->all());

        //Send Confirmation Email
        Mail::send('emails.employee_confirmation',['employees'=>$employee,'acode'=>$acode], function ($message) use ($employee) {
            $message->from('markernest.matute@gmail.com', 'YML Gen. Merch.');
            $message->to($employee->email)->cc('markernest.matute@gmail.com');
        });
        return response(['valid'=>true]);
    }

HELP ME PLEASE

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

Reply to

Use Markdown with GitHub-flavored code blocks.