初始化ajax对象 InitAjax.js
/* 初始化一个xmlhttp对象 */
function InitAjax(){//ajax初始化
var ajax=false;
try{
ajax=new ActionXObject("Msxml2.XMLHTTP");
}catch(e){
try{
ajax=new ActionXObject("Miscrosoft.XMLHTTP");
}catch(e){
ajax=false;
}
}
if(!ajax&&typeof XMLHttpRequest!='undefined'){
ajax=new XMLHttpRequest();
}
return ajax;
}
===================================
function InitAjax(){//ajax初始化
var ajax=false;
try{
ajax=new ActionXObject("Msxml2.XMLHTTP");
}catch(e){
try{
ajax=new ActionXObject("Miscrosoft.XMLHTTP");
}catch(e){
ajax=false;
}
}
if(!ajax&&typeof XMLHttpRequest!='undefined'){
ajax=new XMLHttpRequest();
}
return ajax;
}
调用页面 index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="InitAjax.js" type="text/javascript"></script>
<script type="text/javascript">
//获取新闻(AJAX的GET方式)
function getNews(newsID)
{
//如果没有把newsID参数传过来
if(typeof(newsID) == 'undefined')
{
return false;
}
var url = "show.php?tag=show&></div>
</body>
</html>
============================
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="InitAjax.js" type="text/javascript"></script>
<script type="text/javascript">
//获取新闻(AJAX的GET方式)
function getNews(newsID)
{
//如果没有把newsID参数传过来
if(typeof(newsID) == 'undefined')
{
return false;
}
var url = "show.php?tag=show&></div>
</body>
</html>
服务端执行页面 show.php
<?php
include_once 'DB.class.php';
$tg = new DB('localhost', 'root', 'wangxiang', 'fenghua', 'UTF8');
if($_GET[tag]=='show')
{
$sql = "SELECT Id, title from news limit 0," . $_GET[id];
mysql_query("set names 'utf8'");
$item = $tg->getAll($sql);
sleep(1); //暂停1秒
for($i=0; $i<count($item); $i++)
{
echo "<a href=news.php?nid={$item[$i]['Id']}>{$item[$i]['title']}</a><br>";
}
}
else if($_GET[tag]=='add')
{
sleep(2);//暂停2秒
if($_POST[user_name] != '' && $_POST[user_age] != '' && $_POST[user_sex] != '')
{
$sql = "insert into news (title, pic_path, content) values ('".$_POST[user_name]."', '".$_POST[user_age]."', '".$_POST[user_sex]."')";
mysql_query("set names 'utf8'");
$insert_id = $tg->insert($sql);
if($insert_id > 0)
{
echo "插入成功!";
}
}
else
{
echo "资料填写不全";
}
}
?>
include_once 'DB.class.php';
$tg = new DB('localhost', 'root', 'wangxiang', 'fenghua', 'UTF8');
if($_GET[tag]=='show')
{
$sql = "SELECT Id, title from news limit 0," . $_GET[id];
mysql_query("set names 'utf8'");
$item = $tg->getAll($sql);
sleep(1); //暂停1秒
for($i=0; $i<count($item); $i++)
{
echo "<a href=news.php?nid={$item[$i]['Id']}>{$item[$i]['title']}</a><br>";
}
}
else if($_GET[tag]=='add')
{
sleep(2);//暂停2秒
if($_POST[user_name] != '' && $_POST[user_age] != '' && $_POST[user_sex] != '')
{
$sql = "insert into news (title, pic_path, content) values ('".$_POST[user_name]."', '".$_POST[user_age]."', '".$_POST[user_sex]."')";
mysql_query("set names 'utf8'");
$insert_id = $tg->insert($sql);
if($insert_id > 0)
{
echo "插入成功!";
}
}
else
{
echo "资料填写不全";
}
}
?>