vendredi 27 décembre 2019

how to get laravel response the same page after login with ajax?

i have a login form to login a user i want to know about how to get laravel response in ajax success function. if submit the form i was got a object('status':'msg') in http://127.0.0.1:8000/login page. but i want to just redirect user correct page after login with macing alert. please help me to learn laravel with ajax function.

form

<form id="loginForm" method="POST" action="">
       @csrf
 <input id="email" type="email" class="form-control  name="email" value="" 
   required autocomplete="email" autofocus>

 <input id="password" type="password" class="form-control name="password" required 
    autocomplete="current-password">

 <button type="submit" class="btn btn-primary">LOGIN</button>
</form>

ajax: after document ready

$('#loginForm').submit(function(e){
    e.preventDefault();
    var formInput = $(this);
        $.ajax({
                type:'POST',
                url: 'login',
                data: formInput.serialize(),
                dataType: 'json',
                cache: false,
                success:function(status){
                    if(status== "success"){ 
                          alert("your in");
                       }
                        },
                        error:function(status){
                        if(status== "error"){ 
                          alert("no data found");
                       }
                        }
                    })
                });     

Route:

Route::post('login','loginController@login')->name('loginData'); 

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use Session;
class loginController extends Controller
{
    public function login(Request $request){
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
          //  print_r($request->all());

            session()->put('role', Auth::user()->Role);

            $request->session()->flash('message', 'New customer added successfully.');
            $request->session()->flash('message-type', 'success');
           return response()->json(['status'=>'success']);
           return back();

        }else{
            $request->session()->flash('message', 'you have entered an invalid email address or password. please try again');
            $request->session()->flash('message-type', 'danger');
            return response()->json(['status'=>'error']);
            return back();
        }

    }
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2F3xWqh
via IFTTT

Aucun commentaire:

Enregistrer un commentaire