저는 PHP를 호출하는 아약스를 호출하는 자바 스크립트에서 다음 함수를 가지고 있습니다. 자바 스크립트 함수에 매개 변수를 전달하고 싶습니다. 원하는 PHP 페이지 액세스하려면 div id는 모두 html 코드이며 다음 코드를 사용합니다.아약스와 php로 자바 스크립트 함수가 작동하지 않습니다.
/------------------------- ------------------------- 자바 스크립트 ------------------------ ----------/
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>
내가 당신이 원하는 경우에 당신이 바퀴를 다시 발명 할 수없는 당신에게 않을거야,
이
아약스 js 라이브러리를 직접 작성 하시겠습니까? 그렇지 않으면 jQuery를 다운로드하십시오. $ (document) .ready (function() { $ ("# ResponseDiv"). load ("ajax.php"); }); – DefyGravity
가정에서 성장한 아약스 처리기를 사용하지 마십시오. jquery 또는 mootools를 사용하십시오. 그들은 너의 삶을 훨씬 더 쉽게 만들거야 /. –