login Ajax in Laravel 5.7
Add in header :
<meta name="csrf-token" content="{{ csrf_token() }}">
Add in web.php
Route::group(['as' => 'user.'], function() {
Route::post('login_manual', 'UserController@login_manual')->name('login.post');
});
HTML:
<form class="modal-content animate" id="login-form" >
<input type="text" placeholder="Enter email address" name="email" required class="user-name" id="email" >
<input type="password" placeholder="Enter Password" id="password" name="password" required class="password">
<div class="login-btn"><button type="submit" class="ajax_login" >Login</button></div>
</form>
Ajax code :
<script type="text/javascript">
$('#login-form').submit(function (event){
// alert($("#password").val());
event.preventDefault();
var results = '';
jQuery('#login_error').html('');
$.ajax({
type: 'POST',
url: '{{ route ('user.login.post') }}',
data: {email: $("#email").val(), password:$("#password").val()},
dataType: "json",
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function (response) {
//var response = JSON.parse(response);
console.log(response);
if(response.success == false){
jQuery.each(response.errors, function(key, value){
jQuery('#login_error').show();
jQuery('#login_error').append('<p>'+value+'</p>');
});
} else {
window.location.href = response.redirectto;
}
}, error: function (xhr, status, error) {
jQuery('#login_error').show();
jQuery('#login_error').append('<p>'+error+'</p>');
}
});
});
</script>
Make a usercontroller.php add paste below code in it
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Auth;
class UserController extends Controller
{
//
public function login_manual(Request $request) {
$validator = \Validator::make($request->all() , [
'email' => 'required|email|max:255',
'password' => 'required',
]);
if($validator->fails()) {
return response()->json(['success' => false, 'errors' => $validator->errors()->all()]);
} else {
// create our user data for the authentication
$userdata = array(
'email' => \Input::get('email'),
'password' => \Input::get('password')
);
// attempt to do the login
if (Auth::attempt($userdata)) {
return response()->json(['success' => true, 'redirectto' => '']);
} else {
return response()->json(['success' => false, 'error' => ['Login Failed! Username and password is incorrect.']]);
}
}
}
}
This comment has been removed by the author.
ReplyDeleteWonderful blog & good post on ajax.Its really helpful for me, awaiting for more new post. Keep Blogging!
ReplyDeleteGreat post.
ReplyDeletehttp://forum.yealink.com/forum/member.php?action=profile&uid=128657
Great post.
ReplyDeletehttp://animationindustry.eklablog.com/
Informative post! I really like and appreciate your work, thank you. Thanks for continually sharing such useful information on detector.
ReplyDeleteLOGIN AJAX IN LARAVEL 5.7
ReplyDeleteThis article is a great article that I have seen in my blog career so far, it helps a lot LOGIN AJAX functionality IN LARAVEL, and will continue to do so in the future.
website development company in Surat Gujarat
Meraki solution hub is a Social Media Marketing Company in Surat that creatively tailors your content and enhances your business presence online.
ReplyDeleteHow To Insert Password And Confirm Password Using Ajax In Laravel
ReplyDeleteUsing Ajax is another way to insert Passsword with Json request in Laravel. Here you can see, we are using controller, model and send response to ajax via Json..
For More Info:- How To Insert Password And Confirm Password Using Ajax In Laravel