Categories

Friday, November 22, 2024
#919814419350 therichposts@gmail.com
AjaxLaravel

How to check Email already exists in laravel with ajax?

Laravel 7.2 routing with route group auth guard check with prefix

check Email already exists in laravel with ajax

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to check Email already exists in laravel with ajax? I am doing with laravel first time in my website. Laravel is one of the top php mvc framework. I always share on my blog that what I like and what i faced in my past. This post is also related to my that. In this post we will validate email with ajax. Here is the working code to check Email already exists in laravel with ajax:

First Here is the html code and jquery script and you need to add this into your user.blade.php template:

<input class="form-control" id="cemail" type="email" name="email" placeholder="mail@example.com" onblur="checkmain(this.value)">

/*Js script*/

function checkmain(email)
{
$.ajax({
url: Laravel.appUrl+'/calendar/checkemail',
type: 'POST',
data: { email: email },
}).done(function(response) {
if(response == "Email Already In Use.")
{
alert("Email Already In Use.");
}

});
}

Here is code for laravel Route and you need to add this into you you can routes/web.php file:

Route::post('/checkemail', 'UserController@checkemail')-&gt;middleware('ajax');

And Finally here is the controller code and you need to add this into your UserController.php file:

public function checkemail(Request $req)
{

$PDO = \DB::connection()->getPdo();

$email = $req->email;
$emailcheck = $PDO->prepare("SELECT * FROM user WHERE email = ?");
$emailcheck->execute([$email]);
$emailcheck = $emailcheck->fetchAll(\PDO::FETCH_OBJ);
if(count($emailcheck)>0)
{
echo "Email Already In Use.";
}

//or
public function checkemail(Request $req)
{
$email = $req->email;
$emailcheck = DB::table('user')->where('email',$email)->count();

if($emailcheck > 0)
{
echo "Email Already In Use.";
}
}
}

 

If email will exists then you will alert with ajax There are so many code tricks in laravel and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

8 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.