0

hi, im having a problem using this jquery
I mamanged it to g and fetch data from mysql n now i want it to disply only one class at a time on available or not available
here is the js

$(document).ready(function(e) {
    $('#txtAuction').keyup(function(){

        check_availability();
    });
}); 
    function check_availability()
    {
    var new_Auction =$('#txtAuction').val();
    $.post('checkAuction.php',{txtAuc:$('#txtAuction').val()},
        function(result){
            if(result==1)
            {
             $('h4.alert_success').css("display","block");
             $('h4.alert_success').html(new_Auction + ' is Available');  
            //$('h4.alert_success').fadeOut(5000);   
            }
             else
             {
                $('h4.alert_error').css("display","block"); 
            }   $('h4.alert_error').html(new_Auction + ' is not available'); 

        }




);
}

it is working fine, exept that it shows me both the class.
please healp

thank you a lot
Ajay

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by Airshow
0

If you are checking every time the keyup event fires, then at some point, I would imagine that both blocks of code in your If..else will run. So, for example, say the first time you pass through the if..then and its false, the h4.alert_error runs, and you change the display property to block. On the next keyup, its true so the h4.alert_success block runs. Now you have both showing on the screen.

0

Well, maybe in the success section add..
$('h4.alert_error').css("display","none");

Then in the error section add...
$('h4.alert_success').css("display","none");

0

Displaying the right message requires a fairly trivial reorganisation of the code.

Another, more significant problem is that sequential ajax responses are not guaranteed to arrive back in the same order as the requests were sent. This is particularly likely when requests are issued rapidly (eg. in response to key strokes) and could result in the wrong message being displayed agaonst the current value of #txtAuction.

This potential problem can be overcome by rejecting the immediately previous request before making a new one.

Try this :

$(document).ready(function(e) {

    var chk_avail_xhr = null; //variable for storing the latest request.

    $('#txtAuction').on('keyup', function() {
        var new_Auction = $(this).val();
        if(chk_avail_xhr) {
            chk_avail_xhr.reject(); //inhibit processing of a successful response from the previous request.
        }
        chk_avail_xhr = $.post(
            'checkAuction.php',
            { txtAuc: new_Auction },
            function(result, textStatus, jqXHR) {
                var text = (parseInt(result) == 1) ? ' is Available' : ' is not Available';
                $('h4.alert_success').text(new_Auction + text).show();
            }
        );
    });
});
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.