ajax php验证php代码无法正常工作

  <?php
 if(isset($_POST["username"]) && $_POST["username"] != "")
  {
     $username= $_POST['username'];
    if (strlen($username) < 4) {
   echo '4 - 15 characters please';

  }
if (is_numeric($username[0])) {
 echo 'First character must be a letter';

}
 }
 ?>

php code not working: please help me validation using java script or ajax

e<script type="text/javascript" language="javascript">
   function callme()
 {
var showme = document.getElementById("show");
var user = document.getElementById("uname").value;
    //for check new browser show ajax from
 if(user!=="")
 {
 showme.innerHTML=' loading.....';
   var hr = new XMLHttpRequest()
   {
   hr.open("post","index.php",true);
   hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  hr.readystatechange=function()
 {
  if(hr.readystate== 4 && hr.status==200) {
  showme.innerHTML= hr.responseText;
  }
  } 
  var v="username="+ user;
   hr.send(v);  
  }
}
}
</script>

   <body>
   <span>username:</span>
     <input type="text" name="uname" id="uname"    onBlur="callme();"/>
   <div id="show"></div>
   </body>

all code working fine only php code not working please help me when we enter some text in textbox only loading..... any type of validation are not showing ...

I think your problem is here hr.readystatechange. What you need is hr.onreadystatechange

Maybe this is not the solution but you can echo or print_r or in a file what you receive from the browser in php and see if $_POST['username'] is coming with this name and no with uname.

print_r($_POST);

Your code is very old, now you can use jquery to make things easy with javascript and for php there are lots of frameworks out there that can do your php easy. Tags like onclick into html are deprecated or very near to be.

Is better if you expect an string

$_POST["username"] !== ""

Try to store the message in one variable and at the end of the php function, return it, is better to have only one exit than multiple along the function.

Next time try to put the question and the code in order, for readability and understanding.

Thanks, and sorry for my english... XD