1

I am trying to display a loading.png graphic in the very short time it takes to do my query e.t.c Everything below works perfectly currently. I just need to get the graphic loading in the td.available only when the productcode has been entered and is being checked.

Any ideas?

Here is my JS

$("#prodcodecheck").blur(function() {
        var $this = $(this);
        $this
            .closest('tr') // find the parent tr
            .find('td.available') // find the imgsample in the row
            .html( $(this).attr('id')) // update the contents
            //.animate({'opacity':1},200);

        var available = $this.closest('tr').find('td.available')

        $.post('checkstock.php', //this page reads the image code and gives you the image location
                 { action: 'searchimage', productcode: $(this).val() },
                function(data) {available.html(data);}
                );
    });

And here is checkstock.php

//Find Stock Value
function checkstock($prodCode) { 

  $prodCode = strtoupper($prodCode);

  require '../../../../config.php';
  $dbh = new PDO(DB_DSN, DB_USER, DB_PASS);
  $sql = "SELECT * FROM isproducts WHERE prodCode = '".$prodCode."'"; 
  $stmt = $dbh->query($sql);
  $obj = $stmt->fetch(PDO::FETCH_OBJ);

  $count = $stmt->rowCount();

  echo ($count == 1 ? 'The current stock value of item '.$obj->prodCode.' is '.ROUND($obj->FreeStockQuantity, 0).'' : 'Invalid product code');  

}

//Call Stock Function
checkstock($_POST['productcode']);

3 Answers 3

1

A simple way to do it would be:

...
// reveal a 'loading' div/span or whatever right before the request
$("#loading").show();
$.post('checkstock.php', { action: 'searchimage', productcode: $(this).val() },
    function(data) {
        available.html(data);

        // hide it again once the request has completed
        $("#loading").hide();
    }
);
Sign up to request clarification or add additional context in comments.

Comments

0

You create a jQuery selector that contains the loading img and append it to the td before the ajax code.

var loadingImg = $('<img src="/assets/images/loading.gif">');

available.append(loadingImg);

Comments

0

Why don't you use Jquery load or ajax? And before launching the load function place the loading image inside.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.