这是我编写的AJAX代码:
这是第27页代码:27.PHP
<script language=javascript>
function InitAjax()
{
     var ajax=false;
     try 
    {
         ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) 
    {
          try 
          {
                 ajax = new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (E) 
          {
                 ajax = false;
          }
}
if (!ajax && typeof XMLHttpRequest!='undefined') 
{
       ajax = new XMLHttpRequest();
}
return ajax;
}
var ajax = InitAjax();
ajax.open("POST", "28.php?id=3", true);
ajax.send(null);
</script>
这是第28页代码:28.php
<?php
 $conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误");
 mysql_select_db("test", $conn);
 $id = $_POST['id'];
 $sql="INSERT INTO te (id) VALUES ($id)";    //te 这里是 test下一个表只有一个字段id
 mysql_query($sql);
?>
我只运行27.php时28.php好像根本没作用,数据库中查不到添加的信息。这代码可能不规范,如果不好改,请求有没有类似可用的实例,给小弟研读一下,呵呵!

解决方案 »

  1.   


    ajax.open("POST", "28.php", true);//这句开始
    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST方式,需要
    ajax.send("id=3");//传参放这里
      

  2.   


    $id = $_POST['id'];
    改为$id = $_GET['id'];
      

  3.   

    之前先单独运行一下28.php看看有没有返回值。
    然后你先不要post或get id。自己设一个看看有没有返回值。
    然后再从27.php传值过去看看
      

  4.   

    给你个代码吧,我也是自己练习的:
    这是html:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>在此处插入标题</title>
    <script type="text/javascript">
    var xmlHttp=null;
    function showhit(str){

    if(str.length==0){
    document.getElementById("special").innerHTML="";
    return;
    }
    xmlHttp=GetXMLHttpObject();

    if(xmlHttp==null){
    alert("Your broswer does not support HTTP Request!");
    return;
    }
    var url="special.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=statuChange;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null); 

    }
    function statuChange(){
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
    document.getElementById("special").innerHTML=xmlHttp.responseText;
    }
    } function GetXMLHttpObject(){

    try{
    xmlHttp=new XMLHttpRequest();
    }catch(e){
    try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
    xmlHttp=new ActiveXObject("Microft.XMLHTTP");
    }
    }
    return xmlHttp;
    }
    </script>
    </head>
    <body>
    <form>
    <label style="font-size: 20px;">username:</label>
    <input type="text" id="nameTabel" onkeyup="showhit(this.value)"/><br>
    <label>suggestion:</label><span id="special" style="color:red;font-size:20px;"></span>
    </form>
    </body>
    </html>下面是处理php:<?php$a[]="yulongxinag";
    $a[]="sunxiaomeng";
    $a[]="yangcheng";
    $a[]="lanzhiyu";
    $a[]="zhengwenbo";
    $a[]="luoshicong";
    $a[]="liuxingping";
    $a[]="zhangjinxiong";
    $a[]="wangmaolin";
    $a[]="lianfeng";
    $q=$_GET["q"];//lookup all hints from array if length of q>0
    if (strlen($q) > 0)
    {
    $hint="";
    for($i=0; $i<count($a); $i++)
      {
      if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
        {
        if ($hint=="")
          {
          $hint=$a[$i];
          }
        else
          {
          $hint=$hint." , ".$a[$i];
          }
        }
      }
    }//Set output to "no suggestion" if no hint were found
    //or to the correct values
    if ($hint == "")
    {
    $response="no suggestion";
    }
    else
    {
    $response=$hint;
    }//output the response
    echo $response;
    ?>不知道对你有没有帮助
      

  5.   

    28.php单独给它加参数是有值的
      

  6.   

    ajax.open("POST", "28.php?id=3", true);
    ajax.send(null);
    这里对吗?post应该在send里传值吧。
    ajax.open("POST", "28.php", true);
    ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    ajax.send("id=3");