0

I am new js learner, and I am trying to build a form. in the form I want to check if the input username is already taken or exists and I have done this by laravel controller and lil JS code but I want to HIDE the submit button if the input username is already exists in the database. so how do I do this? please anybody

This is my JS code

<script type="">

$function get_user_name(id, result) {
  var id = $(id).val();
  $.get("<?php echo e(url('home/get_user_name')) ?>/" + id,
  function (data) {
    $(result).html(data);
  });
}
</script>

Controller =>

**and 
controller =** 

public function get_user_name()
  {
     $username = request()->segment(3);
    $user = DB::table('users')
    ->select('username')
    ->where('username', $username)
    ->count();

    $nam = "username already taken";

    if ($user >0) {
      return $nam;
    }else{
    }
  }

html form =>

    <span id="username_rs" style="color: green; font-weight: bold"></span>//showing the message here,

<input id="username" name="username"

           onchange="checkem('#username', '#username_rs')" >


<button type="submit" class="btn btn-primary">
            {{ __('Register') }}
          </button>

route =>

Route::get('/home/get_user_name/{username}', 'HomeController@get_user_name')->name('checkun');

And url look like =>

http://localhost/home/get_user_name/{input username}

My code is showing if the username is taken or not, but I want to hide the submit button if the username is taken or exists in the database.

2 Answers 2

1

You can return like this:

**and 
controller =** 

public function get_user_name()
  {
     $username = request()->segment(3);
    $user = DB::table('users')
    ->select('username')
    ->where('username', $username)
    ->count();

    if ($user >0) {
      return Response::json(array("found"=>"yes", "message"=>"Username is already taken"));       
    }else{
       return Response::json(array("found"=>"no", "message"=>"Something happens wrong"));
    }
  }

Your Script code looks like:

$function get_user_name(id, result) {
  var id = $(id).val();
  $.get("<?php echo e(url('home/get_user_name')) ?>/" + id,
  function (data) {
     if (data.found == 'yes)
     {
        $("#submit_btn").css("display","none")
     }
  });
}

HTML code looks like:

<span id="username_rs" style="color: green; font-weight: bold"></span>//showing the message here,

<input id="username" name="username"

           onchange="checkem('#username', '#username_rs')" >


<button type="submit" id="submit_btn" class="btn btn-primary">
            {{ __('Register') }}
          </button>
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help but its showing controller error => Undefined property: App\Http\Controllers\HomeController::$output ,, How can i use output in the controller?
I have updated the code. Would you please check it again?
It also help me. Thank you!!
0

I would do it like this:

function get_user_name(id, result) {
  var id = $(id).val();
  $.get("<?php echo e(url('home/get_user_name')) ?>/" + id,
  function (data) {
    //check if we get the message: username already taken
    if(data) {
      $(result).html(data);
      $('input[type=submit]', this).attr('disabled', 'disabled'); //disable button
      $('form').bind('submit',function(e){e.preventDefault();}); //disable form
    } else {
      $('input[type=submit]', this).removeAttr('disabled', 'disabled'); //enable button
      $('form').unbind('submit'); //enable form
    }
  });
}

Comments

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.