[英]How to display load image on html page after selecting and until query is work and when request is complete hide image ?
How to display load image on html page after selecting and until query is work and when request is complete hide image ? 在选择后直到查询正常工作以及请求完成后,如何在html页面上显示加载图像?
I have web page which fetching information from a database 我有一个从数据库中获取信息的网页
When a user selects a user in the dropdown list above, a function called "showUser()" is executed. 当用户在上面的下拉列表中选择一个用户时,将执行一个名为“ showUser()”的函数。 The function is triggered by the "onchange" event:
该功能由“ onchange”事件触发:
<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
The page on the server called by the JavaScript above is a PHP file called "getuser.php". 上面的JavaScript调用的服务器上的页面是一个名为“ getuser.php”的PHP文件。
The source code in "getuser.php" runs a query against a MySQL database, and returns the result in an HTML table: “ getuser.php”中的源代码针对MySQL数据库运行查询,并将结果返回到HTML表中:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
* how to display load image on html page after selecting and until query is work and when request is complete hide image ? * 如何在选择后直到查询正常工作以及请求完成后如何在html页面上显示加载图像?隐藏图像? *
*
I try to use code in <head>
tag: 我尝试在
<head>
标记中使用代码:
<script>
$.ajaxSetup({
beforeSend: function (XMLHttpRequest) {
$("#loading").show();
},
complete: function (XMLHttpRequest, textStatus) {
$("#loading").hide();
}
});
</script>
and 和
<div id="loading">
<p><img src="loading.gif" /> Please Wait</p>
</div>
but it doesn't work on my page 但它在我的页面上不起作用
Use jquery. 使用jQuery。
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function getuser(str){
$.ajax({
url: "getuser.php?q="+str,
beforeSend: function (XMLHttpRequest)
{
$("#loading").show();
}
}).done(function ( data ) {
$("#txtHint").html(data);
$("#loading").hide(); });
}
$(document).ready(function(){
$("select[name='users']").change(function () {
getuser($("option:selected", this).val()); });
});
</script>
</head>
Your body should be like this. 你的身体应该是这样的。
<body>
<select name="users">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.