我在javascript中調用ajax調用php, 我想通過參數傳遞給JavaScript函數,我想要的PHP頁面以訪問和 div id,全部來自html,遵循我的代碼:JavaScript函數與Ajax和PHP不工作,因爲我想
/------------------------- -------------------------的JavaScript ------------------------ ----------/
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()
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
HandleResponse(xmlHttp.responseText);
}
}
xmlHttp.open("GET", "ajax.php", true);
xmlHttp.send(null);
}
function HandleResponse(response)
{
document.getElementById('ResponseDiv').innerHTML = response;
}
/-------------------------- ------------------------ PHP ------------------------- ---------/
<?php
echo "This is a php response to your request!!!!!!";
?>
/------------------------------ -------------------- HTML ----------------------------- -----/
<!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>
謝謝極大...
你有什麼理由要編寫自己的ajax js庫嗎?否則下載jQuery。 $(document).ready(function(){(「#ResponseDiv」)。load(「ajax.php」); }); – DefyGravity
不要做自己的本土ajax處理程序。使用jquery或mootools。他們會讓你的生活更輕鬆/。 –