Solved

Problem with JQuery/Ajax statement not updating div

Posted on 2011-09-27
5
191 Views
Last Modified: 2012-05-12
Hi,
I'm working on a username availability script.
All works fine, it checks, the server responds back either a 1 or a 0 (can see it in firebug).
So all works except its not updating the div with the results.
If my username is too short, it updates, but the server response its not catching.
Any idea why?

 
$(document).ready(function()//When the dom is ready
{
$("#username").change(function()
{ //if theres a change in the username textbox

var username = $("#username").val();//Get the value in the textbox
if(username.length > 3)//if the lenght greater than 3 characters
{
$("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
//Add a loading image in the span id="availability_status"

$.ajax({  //Make the Ajax Request
 type: "POST",
 url: "application/check.php",  //file name
 data: "username="+ username,  //data
 success: function(server_response){

 $("#availability_status").ajaxComplete(function(event, request){

 if(server_response == '0')//if check.php return value "0"
 {
 $("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font>  ');
 //add this image to the span with id "availability_status"
 }
 else  if(server_response == '1')//if it returns "1"
 {
 $("#availability_status").text('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
 }

 });
 }

 });

}
else
{

$("#availability_status").html('<font color="#cc0000">Username too short</font>');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});

Open in new window

0
Comment
Question by:tjyoung
5 Comments
 
LVL 15

Expert Comment

by:Eyal
Comment Utility
why do you put this?

$("#availability_status").ajaxComplete(function(event, request)?

it's not needed
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
why did you put $("#availability_status").ajaxComplete(function(event, request){ inside success callback function?

try this :
$(document).ready(function()//When the dom is ready
{
$("#username").change(function()
{ //if theres a change in the username textbox

var username = $("#username").val();//Get the value in the textbox
if(username.length > 3)//if the lenght greater than 3 characters
{
$("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
//Add a loading image in the span id="availability_status"

$.ajax({  //Make the Ajax Request
 type: "POST",
 url: "application/check.php",  //file name
 data: "username="+ username,  //data
 success: function(server_response){

// $("#availability_status").ajaxComplete(function(event, request){

 if(server_response == '0')//if check.php return value "0"
 {
 $("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font>  ');
 //add this image to the span with id "availability_status"
 }
 else  if(server_response == '1')//if it returns "1"
 {
 $("#availability_status").text('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
 }

// });
 }

 });

}
else
{

$("#availability_status").html('<font color="#cc0000">Username too short</font>');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});

Open in new window

0
 
LVL 17

Expert Comment

by:sonawanekiran
Comment Utility
Try cache: false in your ajax call like

$.ajax({  //Make the Ajax Request
 type: "POST",
 url: "application/check.php",  //file name
 cache: false,
 data: "username="+ username,  //data
.
.
.
.
.
.
0
 
LVL 17

Expert Comment

by:sonawanekiran
Comment Utility
and
instead of
$("#availability_status").text('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');

use
$("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
Although I'm having some trouble yet, its because I've got a bunch of other voodoo in my file. The solution shown works.
Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now