Thursday, July 23, 2020

Ajax Insert Crude with toaster message .???

HTML:

<form action="{{ url('email-registration') }}" method="POST" id="user_register_bng">
                  {{ csrf_field() }}
                    <div class="form-group required-field">
                      <label for="email">Email Address</label>
                      <input type="email" name="email" id="email" class="form-control {{ $errors->has('email') ? 'border-danger' : '' }}" value="{{ old('email') }}" data-validation="email">
                        @if ($errors->has('email'))
                          <span class="text-danger">{{ $errors->first('email') }}</span><br/>
                        @endif
                    </div>
                    <div class="form-group required-field">
                      <label for="password">Password</label>             
                      <input type="password" name="password" id="password" class="form-control {{ $errors->has('password') ? 'border-danger' : '' }}" data-validation="required">
                        @if ($errors->has('password'))
                          <span class="text-danger">{{ $errors->first('password') }}</span><br/>
                        @endif
                    </div>

                    <div class="form-group required-field">
                      <label for="password_confirmation">Retype Password</label>             
                      <input type="password" name="password_confirmation" id="password_confirmation" class="form-control {{ $errors->has('password_confirmation') ? 'border-danger' : '' }}" data-validation="required">
                        @if ($errors->has('password_confirmation'))
                          <span class="text-danger">{{ $errors->first('password_confirmation') }}</span><br/>
                        @endif
                    </div>


                    <button type="submit" id="btnSubmitBng" class="btn btn-block btn-primary mt-2">SIGN UP</button>
                 
                </form>


JS:

<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>

<script type="text/javascript">
    $.ajaxSetup({

        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
           }
       });

       $("#btnSubmitBng").click(function(e){ 

        e.preventDefault();
        var email = $("#email").val(); 
        var password = $("#password").val(); 
        var password_confirmation = $("#password_confirmation").val();


        $.ajax({

           type:'POST',
           url:'email-registration',
           data:{ password:password, email:email,password_confirmation:password_confirmation},
           success:function(data){

               if (data.status == 'success') {
                    toastr.success(data.message);
                } else if (data.status == 'error') {
                    toastr.error(data.message);
                } else {
                    toastr.error(data.message);
                }
             
           }

        });

       });
 
  </script>

web.php:

Route::post('/email-registration', 'Auth\RegisterController@emailRegistration')->name('email-registration');


Controller:

public function emailRegistration(Request $request){

        $this->validate($request, [
            'email' => 'email',
            'password' => 'min:6|required_with:password_confirmation|same:password_confirmation',
            'password_confirmation' => 'min:6'
        ]);

        $existingUser = User::where('email', $request->email)->first();

        if($existingUser){
             return response()->json(['status' => 'erroe', 'message' => 'Your email already exist !']);
        }
       
        $input = $request->all(); 
   
        $input['password'] = Hash::make($request->password);
     
       
        $usertData = User::create($input);
       
         return response()->json(['status' => 'success', 'message' => 'User Registration successfully!']);

    }

No comments:

Post a Comment

Ajax load lage with laravel.

 step-1:  HTML <div class="row">                     <div class="col-lg-12">                           <d...