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')->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
public function checkemail(Request $req)
{
$email = $req->email;
$emailcheck = DB::table(‘user’)->where(’email’,$email)->count();
if($emailcheck > 0)
{
echo “Email Already In Use.”;
}
}
Hi Ajay,
Thank you very much for this post.
Could you please explain in more details the following code line?
url: Laravel.appUrl+’/calendar/checkemail’,
Best regards.
Where I have written that?
You wrote that in /*Js script*/
that is for path reference.
Thanks