一:html文件(yans.html)

<META http-equiv=content-type content='text/html; charset=utf-8'/>
<META http-equiv=pragma content=no-cache>
<META http-equiv=expires content=0>
</HEAD>
<BODY>
 <input type="button"  name="TEST"  value =" click" onClick="post();">
</BODY>
<script language="javascript" type="text/javascript">
 
 //处理send_request返回信息的函数,使用AJAX时统一调用此函数
 var http,http_return;
 function checkRequest() {
  if (http.readyState==4 && http.status==200) { // 判断对象状态,信息已经成功返回,开始处理信息
   http_return = http.responseText;
  }
 }
 function post()
 {
  try{
   http= new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e)
  {
   try{
    http= new ActiveXObject("Microsoft.XMLHTTP");
   }catch(e)
   {
    alert("create requestHTTP false!");
    return ;
   }
  }
  // escape(),encodeURI();
  http.open("POST","AJax.php?name="+encodeURI("闫生"),true);

  //只有是post提交的时候使用,下面一句话。get不要
  http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  http.onreadystatechange = checkRequest;

  // 这句话话如果是get提交的话,就发送null即可

 // post提交的时候,相当于提交一个form
  http.send("sex=按钮");
  alert(http_return);
 }
</script>
</HTML>

二:ajax文件(AJax.php)

<?php
function fn_getParam($sSource,$sReplace="")
  {
    $sValue="";
    if (isset($_GET[$sSource])) {
        $sValue = $_GET[$sSource];
    } else if (isset($_POST[$sSource])) {
        $sValue = $_POST[$sSource];
    }
    if (is_null($sValue) || $sValue=="") {
        return $sReplace;
    }
    return $sValue;
  }
$a = fn_getParam('name','yans');
// 打开文件,获得句柄
$handle = fopen('test.txt', 'w+');
// 写入文件
fwrite($handle,$a);

// 关闭文件
fclose($handle);

// 返回ajax操作结果。
echo $a;
?>

posted on 2008-11-25 18:01  Stym--闫生  阅读(236)  评论(0编辑  收藏  举报