html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。相关的知识,希望对你有一定的参考价值。
<html>
<head>
<title>
PHP Demos
</title>
<style>
body{ font-family:"Arial"; }
</style>
<script type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(URL)
{
var ajaxRequest; // The variable that makes Ajax possible!
try
{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer Browsers
try
{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
// Something went wrong (User Probably Doesn't have JS or JS is turned off)
alert("You Browser Doesn't support AJAX.");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function()
{
//This if satement will check if the status of the script
//If the readyState is not equal to 4 (The request is complete)
//It will display text that says loading...
if(ajaxRequest.readyState < 4)
{
//AJAX in the prenthacies, is what id element in the body will be changed.
document.getElementById('AJAX').innerHTML = "<h2>Loading...</h2>";
}
//Once the readyState is equal to four,
//this means that the request was sent,
//and successfully processed.
if(ajaxRequest.readyState == 4)
{
//This is where the output of the file we called and it will be placed
//in the div where we named the ID = AJAX
document.getElementById('AJAX').innerHTML = ajaxRequest.responseText;
}
}
//This section processes the data
ajaxRequest.open("GET", URL, true);
ajaxRequest.send(null);
}
//-->
</script>
</head>
<body>
<!-- This is the link to the ajax function
It should always start with BLOCKED SCRIPT
after that is the function name ajaxFunction
inside the parenthacies, is the file we want,
this file can look like any URL you would make in a
normal href. Examples:
date.php
date.php?page=1
date.php?page=1&name=fred
-->
<a href="BLOCKED SCRIPTajaxFunction('ajaxdate.php')">
What Time Is It?
</a>
<div id="AJAX">
</div>
<p>
<strong>
<a href="http://www.brighthub.com/hubfolio/matthew-casperson/blog/archive/2009/11/01/php-tutorials.aspx">
Return to the tutorial index
</a>
</strong>
</p>
</body>
</html>
//The ajaxdate.php script will return a message depending on the current time.
<?php
$AntePost = date("A");
$time = date("g:i:s");
if($AntePost == "AM")
{
echo 'Good Morning!';
}
else
{
echo 'Good Evening!';
}
?>
以上是关于html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。的主要内容,如果未能解决你的问题,请参考以下文章