0

Username available check in my register form if I am enter the username after loader.gif is loading but I am not getting the result for username available or not.. give me any suggestion

This is My Controller:

public function name()
{
    $username = Input::get('username');
    $users = DB::table('user')
               ->where('username', $username)
               ->first(); 
    if ( $users !== null )
    {
        return true;
    }
         return false;
         return view('test/username');
}

This is My Route:

Route::get('test/name', 'PageController@name');

This is My Blade Template Ajax:

  <script type="text/javascript">

     $(document).ready(function(){
        $("#username").change(function(){
             $("#message").html("<img src='../images/loader.gif' /> checking...");


        var username=$("#username").val();
          //alert(username);
          $.ajax({
                type:"post",
                 dataType: "json",
                url :"{{URL::to('test/name') }}",
               data: {username: username},
                    success:function(data){
                    if(data==0){
                        $("#message").html("<img src='../images/yes.png' /> Username available");
                    }
                    else{
                        $("#message").html("<img src='cross.png' /> Username already taken");
                    }
                }
             });

        });

     });

8
  • Please see the console and you able to ajax process Commented Mar 16, 2016 at 6:41
  • i have seen console i am getting 500 internal server error... Commented Mar 16, 2016 at 6:48
  • can you please update your question with 500 internal server error description Commented Mar 16, 2016 at 6:49
  • hi solve this problem for 500 internal server error.. but i am not getting the username availbale or not.. now loader.gif only loading.. in my console i am getting the whatever i enter the name is coming like localhost/laravel-test/public/test/username?username=murali Commented Mar 16, 2016 at 6:57
  • Sorry I'm not getting you Commented Mar 16, 2016 at 6:59

1 Answer 1

1

ajax is not response to the controller this is my problem

This is because you need to send json using the response() method from the controller to your view file.

Try out this way:

Controller:

public function name(Request $request)
{
    // your validation logic ..

    $userFound = User::find($request->input('username'))

    if($userFound !== null) {
        return response([
            'status'  => 'failed',
            'message' => 'User Not Found'
        ]);
    }

    return response([
        'status'  => 'success',
        'message' => 'User Found'
    ]);
}

And then update your success method of AJAX handler:

$.ajax({
    // ..

    success: function(receivedData) {
        console.log(receivedData);
    }

    // ..

});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks i tried your suggestion but it is not working
in my ajax not responding from controller select the username from database... how to call controller in this php query like $username=$_POST["username"]; $query=mysql_query("SELECT * from user where username='$username' "); $find=mysql_num_rows($query); echo $find;

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.