0

I am trying to implement ajax in laravel framework, where I am trying to check for the availability for the username, I was able to implement the same via core php but not able to get through laravel 4.

1 Answer 1

1

1) You have to define a method in your controller (i.e UserController)

public function postEmail()
{
    $userCount = User::where('email', '=', Input::get('email'))->count();
    if ($userCount == 0) 
    {
        return "true";
    } else {
        return "false";
    }
}

2) In your registration form you have to do jQuery validation like this to make a remote call on Controller method

$(document).ready(function() {
    $('#registration-form').validate({
        rules: {
            email: {
                required: true,
                email: true,
                remote: {
                    type: "POST",
                    url: '/user/email'
                    }
                }
            },
            messages: {
                email: {
                    remote: "The email has already been taken!"
                }
            },           
            success: function(date) {
                console.log(data);
            }
        });
Sign up to request clarification or add additional context in comments.

2 Comments

what is url: '/user/email'
this is url to access/ make a request the method for that first you have to define a route in routes.php Route::post('user/email', array( 'as'=> 'userCheck', 'uses'=>'UsersController@postEmail' ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.