2011-07-27 48 views
1

我在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> 

謝謝極大...

+6

你有什麼理由要編寫自己的ajax js庫嗎?否則下載jQuery。 $(document).ready(function(){(「#ResponseDiv」)。load(「ajax.php」); }); – DefyGravity

+1

不要做自己的本土ajax處理程序。使用jquery或mootools。他們會讓你的生活更輕鬆/。 –

回答

2

別擔心,我不會告訴你,你不能,如果你想重新發明輪子至。 ^^

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 body組件。注意我使用了「引號」而不是「撇號」。

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

kkkkkkkkk,完美的Tanoro,非常感謝你,我無法下載圖書館,因爲重點是表演,它是一個管理小組.. ..你救了我的一天,hehehe –