Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with JQuery/Ajax statement not updating div

Posted on 2011-09-27
5
194 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
ID: 36708596
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
ID: 36708613
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
ID: 36708614
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
ID: 36708624
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
ID: 36709439
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…

791 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