显示加载图像php

I am trying to show loading image until php excutes, the query works on the second page but the results aren't showing on the first page, I know I am missing something here, can someone help me out? I am new to jquery or ajax thing.

home.php

<html>
<head>
<!--Javascript-->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$('#loading_spinner').show();

var post_data = "items=" + items;
$.ajax({
    url: 'list.php',
    type: 'POST',
    data: post_data,
    dataType: 'html',
    success: function(data) {
        $('.my_update_panel').html(data);
    },
    error: function() {
        alert("Something went wrong!");
    }
});

$('#loading_spinner').hide();
</script>
<style>
   #loading_spinner { display:none; }
</style>
</head>
<body>

<img id="loading_spinner" src="image/ajax-loader.gif">

<div class="my_update_panel">

<!--I am not sure what to put here, so the results can show here-->

</div>

list.php I tested the query and it prints the rows.

<?php
   include_once("models/config.php");

   // if this page was not called by AJAX, die
   if (!$_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') die('Invalid request');

   // get variable sent from client-side page
   $my_variable = isset($_POST['items']) ? strip_tags($_POST['items']) :null;  
       //run some queries, printing some kind of result
   $mydb = new mysqli("localhost", "root", "", "db");
   $username = $_SESSION["userCakeUser"];

       $stmt = $mydb->prepare("SELECT * FROM products where username = ?");
   $stmt->bind_param('s', $username->username);
   $stmt->execute();
   // echo results

   $max = $stmt->get_result();
   while ($row = $max->fetch_assoc()) {
      echo $row['title'];
       echo $row['price'];
      echo $row['condition'];

   }

?>

There is a issue with

var post_data = "items=" + items;

Make it

$(document).ready(function(){
        $('#loading_spinner').show();
$.ajax({
    url: 'list.php',
    type: 'POST',
    data: {"items":items},
    dataType: 'html',
    success: function(data) {
        $('.my_update_panel').html(data);
        $('#loading_spinner').hide();     
    },
    error: function() {
        alert("Something went wrong!");
    }
});
});

let me know if works for you.

The HTML. Put the img inside of the .my_update_panel div

<div class="my_update_panel">
    <img id="loading_spinner" src="image/ajax-loader.gif">
</div>

The JS

var url = 'list.php';
var post_data = "items=" + items;

$('.my_update_panel').load(url, post_data, function() {
    $(this +' #loading_spinner').fadeOut('slow');
});

You can find a good selection of loading images that are readily available for download here.

Try To add complete event in the ajax , I hope it will work. Refer http://api.jquery.com/jQuery.ajax/

<html>
<head>
<!--Javascript-->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$('#loading_spinner').show();

var post_data = "items=" + items;
$.ajax({
    url: 'list.php',
    type: 'POST',
    data: post_data,
    dataType: 'html',
    success: function(data) {
        $('.my_update_panel').html(data);
    },
    error: function() {
        alert("Something went wrong!");
    },
    complete:function(){
           $('#loading_spinner').fadeOut(500);
});

//$('#loading_spinner').hide();
</script>
<style>
   #loading_spinner { display:none; }
</style>
</head>
<body>

<img id="loading_spinner" src="image/ajax-loader.gif">

<div class="my_update_panel">

<!--I am not sure what to put here, so the results can show here-->

</div>

It is better to chain the ajax callback functions. adding callbacks as options will be removed from jquery in the future.

http://api.jquery.com/jQuery.ajax/

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

var post_data = items;
$('#loading_spinner').show();
$.ajax({
    url: 'list.php',
    type: 'POST',
    dataType: 'html',
    data: {"items":post_data},
})
.done(function() {
    console.log("success");
})
.fail(function() {
    console.log("error");
})
.always(function() {
    $('#loading_spinner').hide();
});

I hide the loading_spinner in the always() callback, that way the spinner also disappears when the ajax call throws an error. if this is not working you have to search in your server side code to solve the problem. First of, are you sure the response is html? you might have to set the dataType to text in the ajax call.