Add a field based on the username

Discussion in 'JavaScript' started by namooth, Dec 31, 2006.

  1. #1
    Hey all,
    I'm looking for a script (Ajax or javascript) that will help me categorize my web users. what i want to accomplish is:
    there will be 2 fields in the login screen, "user name" and "password", now, if the user name types a username that currently isn't in my database a third field will appear asking for a secondary password and the submitting will refer to a secondary database. (the data search will take place behind the scenes when the user will move from the username field to the password field)
    to start i want to create 2 fields, if the text in the first field = the string "testing" , a third field will appear.
    thanks
     
    namooth, Dec 31, 2006 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    File : index.php ( or whever your form is )
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Example - Happy New Year !</title>
    <script language="javascript">
    function processAjax( )
    {
    	xmlHttp=GetXmlHttpObject()
    	var url="ajax.php"
    	url=url+"?username="+ encodeURI( document.getElementById('username').value )
    	url=url+"&sid="+Math.random()
    	xmlHttp.onreadystatechange=StateChanged
    	xmlHttp.open("GET",url,true)
    	xmlHttp.send(null)
    }
    function StateChanged() 
    { 
    	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    	{ 
    	document.getElementById('extra').innerHTML=xmlHttp.responseText 
    	}
    }
    function GetXmlHttpObject()
    { 
    var objXMLHttp=null
    	if (window.XMLHttpRequest)
    	{
    		objXMLHttp=new XMLHttpRequest()
    	}
    	else if (window.ActiveXObject)
    	{
    	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    	}
    return objXMLHttp
    } 
    </script>
    </head>
    
    <body>
    <form action="" method="post">
    Enter Username : &nbsp; <input type="text" 
    							  name="username" 
    							  id="username"
    							  onblur="javascript :processAjax( );"
    							  />
    							  <br />
    Enter Password : &nbsp; <input type="password" name="password" /><br />
    <span id="extra"></span>
    </form>
    </body>
    </html>
    
    PHP:
    The file above can be saved as html also, indeed anything that can execute js

    Filename : ajax.php <-- important name, leave as is ...

    
    <?
    if($_GET['username'] == "testing")
    {
    // Condition for if the username exists in the database
    echo "<input type=\"submit\" value=\"Login\"><br/>\n";
    }
    else
    {
    // Condiion for if the username does not exist in the database
    echo "Enter Password Again :<input type=\"password\" name=\"password2\"><br />\n".
    	 "<input type=\"submit\" value=\"Register\">";
    }
    ?>
    
    PHP:
    Obviously you'll need to add the database bits, but I do believe that's what you're looking for, happy new year :)
     
    krakjoe, Jan 1, 2007 IP
  3. namooth

    namooth Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Dude!!! :eek:
    Thats wicked!
    thank you so much!
     
    namooth, Jan 1, 2007 IP