ajax not working when calling php
This my jquery script:
<script>
$(document).ready(function(){
$("#username").blur(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
//check the username exists or not from ajax
$.post("check.php",{ user_name:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
});
}
});
});
});
</script>
This is my php script:
<?php
header("Content-Type: text/plain");
$username=$_POST["user_name"];
$xml = simplexml_load_file("usernames.xml");
foreach($xml->children() as $child)
{
if ( $child == $username ) {
echo "no";
}
}
?>
And this is my xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<usernames>
<num>bobby</num>
<num>murali</num>
<num>rakesh</num>
<num>manoj</num>
</usernames>
I do not understand where the problem is.I think php is getting called and echoing the correct statement..but my script isnt catching it properly i think.I could not understand where the problem exists.What ever name is enter it just skips the if
loop and enters into else in my jquery script.I have executed the php individually and it works fine.I think problem is with jquery script.My script works fine when i connect to a database and fing the availabili开发者_如何学Cty of username.
Try: { 'user_name' : $(this).val() }
精彩评论