Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] - Ajax or Jquery check username
#1

[eluser]the_unforgiven[/eluser]
I;m wanting to know if any off you have found a solution to check if a username is taken in the database.

For example

I have a username of test so if i signup up again and state my username has test i want ajax or something to say invalid username so it make me change it because it already exsits.

Does anyone have any tuts on this and how i should go about it.
#2

[eluser]Noobigniter[/eluser]
Hello,
I can not find the code that I used to check the Nickname, however I always the one for email, so just change the ID in html, and "filter" in javascript.

That's the code:

// IN MY USERS CONTROLLER
Code:
<?php
function email_disponibilite()
{
  $this->load->model('user_model');
  $get_result = $this->user->email_disponibilite();
  
  if(!$get_result)
   echo '<span class="error">Cet email est déja utilisé.</span>';
  else
   echo '<span class="success">Cet email est disponible.</span>';
}
?&gt;


// IN MY USERS MODEL
Code:
&lt;?php
function email_disponibilite($email = '')
{
  $email = strtolower($this->input->post('email', TRUE));
  $query = $this->db->query('SELECT id FROM users WHERE email = ?', $email);
  
  return ($query->num_rows() > 0) ? FALSE : TRUE;
}
?&gt;

//
// IN MY USERS VIEW
Code:
&lt;form&gt;
      <p>Email:<br />
      &lt;?php echo form_input($email);?&gt;
  <span id="Loading"><img src="&lt;?=base_url();?&gt;images/site/loader.gif" alt="Ajax Indicator" /></span>
  <span id="reponse_dispo_email"></span>
      </p>
&lt;/form&gt;

// JAVASCRIPT
Code:
&lt;!-- insert jQuery/JS files --&gt;
  [removed]
   // Disponibilité email
   $(document).ready(function() {
       $('#Loading').hide();    
       $('#email').blur(function(){
     var a = $("#email").val();
     var filtre = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
     if(filtre.test(a)){
      $('#Loading').show();
      $.post( "&lt;?php echo base_url();?&gt;users/email_disponibilite/", { email: $('#email').val() },
        function(response){
         $('#Loading').hide();
         setTimeout(function(){ finishAjax('Loading', escape(response)); },400);
        }
      );
      return false;
     }
    });
    function finishAjax(id, response){
     $('#'+id).html(unescape(response));
     $('#'+id).fadeIn();
    }
   });
  [removed]

Hope this helps you ...

Cordially.
#3

[eluser]the_unforgiven[/eluser]
Tried that one but didnt seem to work...all i kept getting was the loder.gif imahes all the time
#4

[eluser]Noobigniter[/eluser]
Yes indeed it does not work, but it is how to proceed.
Sorry.
#5

[eluser]the_unforgiven[/eluser]
Ive solved this problem...

Built my own, so thought i;d share

Code:
java in your view file

$(document).ready(function() {

    //the min chars for username
    var min_chars = 3;

    //result texts
    var characters_error = 'Minimum amount of chars is 3';
    var checking_html = className = ".preloader";
  
    //when button is clicked
    $('#check_username_availability').click(function(){
     //run the character number check
     if($('#username').val().length < min_chars){
      //if it's bellow the minimum show characters_error text '
      $('#username_availability_result').html(characters_error);
     }else{
      //else show the cheking_text and run the function to check
      $('#username_availability_result').html(checking_html);
      check_availability();
     }
    });

    });

  //function to check username availability
  function check_availability(){

    //get the username
    var username = $('#username').val();

    //use ajax to run the check
    $.post("&lt;?php echo base_url(); ?&gt;user/check_user", { username: username },
     function(result){
      //if the result is 1
      if(result == 1){
       //show that the username is available
       $('#username_availability_result').html(username + ' is available');
      }else{
       //show that the username is NOT available
       $('#username_availability_resuls').html(username + ' is not available');
      }
    });

  }

Code:
contoller:

function check_user()
{
    $this->user_model->userCheck();
}

Code:
model

function userCheck()
  {
    $username = $this->input->post('username');  

  //mysql query to select field username if it's equal to the username that we check '  
  $result = $this->db->query('SELECT * FROM customers WHERE username = ?', $username);
  //if number of rows fields is bigger them 0 that means it's NOT available '  
  if($result->num_rows() > 0){  
      //and we send 0 to the ajax request  
      echo 0;  
  }else{  
      //else if it's not bigger then 0, then it's available '  
      //and we send 1 to the ajax request  
      echo 1;  
  }
  }

an you will need to stick this in your form

Code:
&lt;input type='button' id='check_username_availability' value='Check Availability'&gt;  
<div id='username_availability_result'></div>
#6

[eluser]Mauricio de Abreu Antunes[/eluser]
Dude, how to procced:

1 - Use a callback javascript function that return wheter user exists or not.
2 - Call some php file (controller method) that checks your input (username).
3 - Return the return (boolean) for your Ajax request.

Tips: use JQuery validate (plugin for JQuery).
Call a controller.
#7

[eluser]Unknown[/eluser]
[quote author="the_unforgiven" date="1329163007"]Ive solved this problem...

Built my own, so thought i;d share

Code:
java in your view file

$(document).ready(function() {

    //the min chars for username
    var min_chars = 3;

    //result texts
    var characters_error = 'Minimum amount of chars is 3';
    var checking_html = className = ".preloader";
  
    //when button is clicked
    $('#check_username_availability').click(function(){
     //run the character number check
     if($('#username').val().length < min_chars){
      //if it's bellow the minimum show characters_error text '
      $('#username_availability_result').html(characters_error);
     }else{
      //else show the cheking_text and run the function to check
      $('#username_availability_result').html(checking_html);
      check_availability();
     }
    });

    });

  //function to check username availability
  function check_availability(){

    //get the username
    var username = $('#username').val();

    //use ajax to run the check
    $.post("&lt;?php echo base_url(); ?&gt;user/check_user", { username: username },
     function(result){
      //if the result is 1
      if(result == 1){
       //show that the username is available
       $('#username_availability_result').html(username + ' is available');
      }else{
       //show that the username is NOT available
       $('#username_availability_resuls').html(username + ' is not available');
      }
    });

  }

Code:
contoller:

function check_user()
{
    $this->user_model->userCheck();
}

Code:
model

function userCheck()
  {
    $username = $this->input->post('username');  

  //mysql query to select field username if it's equal to the username that we check '  
  $result = $this->db->query('SELECT * FROM customers WHERE username = ?', $username);
  //if number of rows fields is bigger them 0 that means it's NOT available '  
  if($result->num_rows() > 0){  
      //and we send 0 to the ajax request  
      echo 0;  
  }else{  
      //else if it's not bigger then 0, then it's available '  
      //and we send 1 to the ajax request  
      echo 1;  
  }
  }

an you will need to stick this in your form

Code:
&lt;input type='button' id='check_username_availability' value='Check Availability'&gt;  
<div id='username_availability_result'></div>
[/quote]

This: var checking_html = className = ".preloader";
Dont work..
It show ".preloader" as text in my DIV tag.
#8

[eluser]the_unforgiven[/eluser]
This: var checking_html = className = “.preloader”;

Is in the css

Code:
.preloader {
background: url('path/to/your/images/preloader.gif');
height: 36px;
width: 36px;
}
as an example.




Theme © iAndrew 2016 - Forum software by © MyBB