www.webdeveloper.com
Results 1 to 3 of 3

Thread: ajax textfield checking

  1. #1
    Join Date
    Aug 2011
    Location
    Hong Kong
    Posts
    49

    Unhappy ajax textfield checking

    i have built a username check on a textfield that when the username already existed in the database there will be an alert

    the javascript runs in onBlur of the textfield

    everything works okay except the textfield check when the textfield is blank (onreadystagechange >>>> change from something to nothing)

    Can anyone please help to fix this bug???? the code is as below:


    Code:
    <script type="text/javascript">
    var xmlHttp;
    function createXHR() {
    	if (window.XMLHttpRequest) {
    		xmlHttp= new XMLHttpRequest();
    	} else if (window.ActiveXObject) {
    		xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	if (!xmlHttp) {
    		alert('Your browser does not support XMLHTTP object');
    		return false;
    	}
    }
    			createXHR();
    			function sendUsername(n) {
    				document.getElementById('alert_username').innerHTML='checking';				 
    				xmlHttp.onreadystatechange=catchUsername;
    				xmlHttp.open('GET','reg.php?timeStamp='+new Date().getTime()+'&search='+n,true);
    				xmlHttp.send(null);				
    			}
    			function catchUsername() {
    				if (xmlHttp.readyState==4) {
    					if (xmlHttp.status==200) {
    						var s=xmlHttp.responseText;
    						if (s==true) {
    							document.getElementById('check_username').innerHTML='';
    							document.getElementById('alert_username').innerHTML='username already been used';
    						} else {
    							document.getElementById('alert_username').innerHTML="";
    							document.getElementById('check_username').innerHTML='<img src="../check.png">';
    						}
    					}
    				}
    			}
    
    
    
    </script>
    
    
    <form>
    <input name="username" id="username" type="text" onBlur="sendUsername(this.value);" value="<?php echo stripslashes(htmlentities($_POST['username'])); ?>"/>
    <span id="check_username"></span>
    <div id="alert_username"></div>
    </form>

    reg.php

    just some php code to connect to the database and retrieve the data and check whether the value from the textfield exist already.
    if yes, echo true, else echo false
    Last edited by jeffap; 06-20-2012 at 10:57 AM.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,633
    may be this will help

    Code:
    function sendUsername(n){
    if(n.length>0){
    document.getElementById('alert_username').innerHTML='checking';
    xmlHttp.onreadystatechange=catchUsername;
    xmlHttp.open('GET','reg.php?timeStamp='+new Date().getTime()+'&search='+n,true);
    xmlHttp.send(null);
    }
    }
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Aug 2011
    Location
    Hong Kong
    Posts
    49
    so simple to have it solved *banghead*

    Thanks padonak!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles