带有ajax和php的javascript函数不能按我所希望的那样工作

带有ajax和php的javascript函数不能按我所希望的那样工作,php,javascript,ajax,forms,Php,Javascript,Ajax,Forms,我在javascript中有以下函数调用ajax调用php, 我想通过参数传递给javascript函数,即我想要访问的php页面和 div id全部来自html,遵循我的代码: /--------------------------------------------------javascript----------------------------------/ /--------------------------------------------------php---------

我在javascript中有以下函数调用ajax调用php, 我想通过参数传递给javascript函数,即我想要访问的php页面和 div id全部来自html,遵循我的代码:

/--------------------------------------------------javascript----------------------------------/

/--------------------------------------------------php----------------------------------/


/--------------------------------------------------html----------------------------------/


PHP AJAX示例
这是一个用来保存响应的div。

非常感谢……

别担心,我不会告诉你,如果你想的话,你不能再发明轮子了^^

function getXMLHttp() 
{
    var xmlHttp

    try
    {
        //Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch(e)
    {
        //Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                alert("Your browser does not support AJAX!")
                return false;
            }
        }
    }
    return xmlHttp;
}

function MakeRequest(page, obj) 
{
    var xmlHttp = getXMLHttp();

    xmlHttp.onreadystatechange = function()
    {
        if(xmlHttp.readyState == 4)
        {
            HandleResponse(xmlHttp.responseText, obj);
        }
    }

    xmlHttp.open("GET", page, true);
    xmlHttp.send(null);
}

function HandleResponse(response, obj)
{
    document.getElementById(obj).innerHTML = response;
}
下面是HTML主体组件。注意,我使用了“引号”而不是“撇号”

<input type="button" onclick="MakeRequest('ajax.php', 'ResponseDiv');" value="Use AJAX!!!!"/>
<div id='ResponseDiv'>
  This is a div to hold the response.
</div>

这是一个用来保存响应的div。

您想编写自己的ajax js库有什么原因吗?否则请下载jQuery$(document.ready(function(){$(“#ResponseDiv”).load(“ajax.php”);});不要自行开发ajax处理程序。使用jquery或mootools。它们会让你的生活变得更轻松。/.kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk。。你救了我一天,呵呵
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <script type='text/javascript' src='ajax.js'></script>
    <title>PHP AJAX Example</title>
  </head>
  <body>
    <input type='button' onclick='MakeRequest();' value='Use AJAX!!!!'/>
    <div id='ResponseDiv'>
      This is a div to hold the response.
    </div>
  </body>
</html>
function getXMLHttp() 
{
    var xmlHttp

    try
    {
        //Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch(e)
    {
        //Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e)
            {
                alert("Your browser does not support AJAX!")
                return false;
            }
        }
    }
    return xmlHttp;
}

function MakeRequest(page, obj) 
{
    var xmlHttp = getXMLHttp();

    xmlHttp.onreadystatechange = function()
    {
        if(xmlHttp.readyState == 4)
        {
            HandleResponse(xmlHttp.responseText, obj);
        }
    }

    xmlHttp.open("GET", page, true);
    xmlHttp.send(null);
}

function HandleResponse(response, obj)
{
    document.getElementById(obj).innerHTML = response;
}
<input type="button" onclick="MakeRequest('ajax.php', 'ResponseDiv');" value="Use AJAX!!!!"/>
<div id='ResponseDiv'>
  This is a div to hold the response.
</div>