tags:

views:

21

answers:

2

I have this code and I would like to modify it if possible to echo an image instead of the "is / not available" text. I'd also like to get rid of the button and use a onkeyup or something close to it so the user has real time notification. Can someone please help me to make these changes?

  $(document).ready(function() {
      $("#btnUsername").click(function() {
          var name = $("#txtUsername").val();
          var status = $("#divStatus");
          status.html("Checking....").removeClass();
          $.post("1.php", { username: name },
          function(data) {
              if (data == "true") {
                  status.html(name + " Is Available!").addClass("green");
              } else {
                  status.html(name + " Is Not Available!").addClass("red");
              }
          });
      });
  });
A: 
...
status.html(name + "<img src='unavailable.png' />").addClass("red");
...
Eimantas
A: 

You could have your 1.php return JSON, which would then be placed in the data object in your callback method. One of the JSON properties could be the url/path to the image which you could add to the screen.. your callback could look something like:

function(data) {
    if (data) {
        status.html($("<img/>").attr("src", data.image_path));
    } else {
        status.html(name + " Is Not Available!").addClass("red");
    }
});

I'm not sure what your 1.php is doing so I'm not sure if this is feasible, but this is how I would do it..

EDIT Unless you had an image you wanted to display (like if its always the same "Success/Fail" image, you could just include the path rather than having your 1.php return it)

As for changing the click thats just a matter of changing

  $("#btnUsername").click(function() {

to

  $("#btnUsername").keyup(function() {
Rabbott
Hi Rabbot, I tried all of your suggestions but none of them work. Eimantas's suggestion did change the image for me though. I would even be able to live with the text, but I really would love to get rid of the button. Are you sure that keyup will work here? Maybe I'm doing something wrong? I just replaced the click for keyup but it won't work
jim
The 1.php is only returning a true or false value for jquery to display the image.
jim
Your question is a little hard to understand.. our solutions are the same the only difference is that mine allows you to specify a path from json (I thought you wanted to display the image if it was found, not just display a 'not found' image) so using keyup, if 1.php returns JSON (one of your values could be success => true) you could specify the images path, and display it using the code I provided. What exactly are you trying to display, a static unavailable image, or the image that you are looking for on success?
Rabbott