AjaxLaravel

How to check Email already exists in laravel with ajax?

check Email already exists in laravel with ajax

Views

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

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

3 Comments

  • public function checkemail(Request $req)
    {
    $email = $req->email;
    $emailcheck = DB::table(‘user’)->where(’email’,$email)->count();

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

Leave a Reply

Positive SSL