Results 1 to 7 of 7

Thread: [RESOLVED] php with javascript? check if username is taken

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Resolved [RESOLVED] php with javascript? check if username is taken

    i'm making a registration form and i want it to check if the username has been taken while the user is registering (before the user submits the form)

    how to do this?

  2. #2
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: php with javascript? check if username is taken

    Just a quick thought

    1) On the onBlur event of the username field
    2) Use xmlhttprequest to fire your username to a php script (e.g. 'http://domain.com/checkUsername.php?username'+element.value)
    3) Using PHP, check the usernames status, return this value
    4) Depending on the result output either a success of failure message

    Sorry I cannot provide an example right now, but Googling these stages should sort you out, post back with any problems you may have.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: php with javascript? check if username is taken

    thats a good tip, i will do some searching and return here

  4. #4
    Fanatic Member bharanidharanit's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    673

    Re: php with javascript? check if username is taken

    Hi,
    I recently made an registration form, and checks for the existing username using AJAX. Try this.
    Code:
    function exists_username(field)
    {
    	if (window.XMLHttpRequest) 
    	{
    	  	XMLHttpRequestObject = new XMLHttpRequest();
    	}
    	else if (window.ActiveXObject) 
    	{
    		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	field = field.value;
    	var url = "functions.php?ud="+ field;
    	if(XMLHttpRequestObject) 
        {
    	  var obj = document.getElementById("v1");  //to display the error message as "Username already taken"
          XMLHttpRequestObject.open("GET", url,false); 
          XMLHttpRequestObject.onreadystatechange = function() 
          { 
            if (XMLHttpRequestObject.readyState == 4 && 
              XMLHttpRequestObject.status == 200) 
              { 
    	        if(XMLHttpRequestObject.responseText=="Username Already Exists")  
    	        {
    				obj.innerHTML = XMLHttpRequestObject.responseText;
    				return false;//but this is not returning as false. 				
    			}
    			return false;
              } 
          } 
          XMLHttpRequestObject.send(""); 
        }
    }
    functions.php

    Code:
    //connection to the host&database
    $query = "SELECT username
    			  FROM users
    			  WHERE username='$ud';";
    	$result = mysqli_query($dbconn,$query);
    	if(mysqli_num_rows($result)>1)
    	{echo "Username Already Exists";}

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: php with javascript? check if username is taken

    I would personally advise you find a tutorial about ajax and use that instead of using any of the code posted above.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: php with javascript? check if username is taken

    yeah thats what i will do too, otherwise i won't learn much of it..

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: php with javascript? check if username is taken

    Quote Originally Posted by kows View Post
    I would personally advise you find a tutorial about ajax and use that instead of using any of the code posted above.
    The code is also vulnerable to SQL injection; uses register globals and sends reduncdant information. Prehaps its best to delete the entire post.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width