2011-04-08 154 views
0

我想知道加載PHP頁面並使用AJAX插入頁面部分的最佳技術?如何使用AJAX加載PHP腳本?

例如,請考慮以下的HTML代碼:

<html> 
<body> 
    <div id="web_Logo"> 
     <h1>Website Logo</h1> 
    </div> 
    <div id="web_Menu"> 
     <ul> 
      <li><a href="index.php">Home</a></li> 
      <li><a href="help.php">Help</a></li> 
      <li><a href="contact.php">Contact</a></li> 
     </ul> 
    </div> 
    <div id="web_Content"> 
     //CONTENT LOADS HERE// 
    </div> 
</body> 
</html> 

什麼將是一個頁面使用AJAX(help.php或contact.php例如)加載到內容事業部段的最好方法?

我正在使用我的網站的JQuery庫,如果有幫助。

+1

請...我求求你...不要用Javascript來做你的整個網站。簡單的服務器端包含PHP很容易。 – Brad 2011-04-08 18:42:19

回答

1

我假設你想點擊一個鏈接時,頁面在web_Content DIV加載,如果是的話,你可以這樣做:

$(function(){ 
    $('#web_Menu a').click(function(e){ 
     e.preventDefault(); 
     $('#web_Content').load($(this).attr('href'), function(){ 
      alert('File loaded'); 
     }); 
    }); 
}); 
0
<script src="jquery.js"></script> 
// dont forget to include jquery script 

<script type="text/javascript"> 

function a() 
{ 

    var file="otherfilename"; //Enter the name of file, which is to be loaded 
    $("#load_file").load(file+".php").fadeIn("slow"); 

} 

</script> 

<button onClick="a();">Read File</button> 

<div id="load_file"> The division where file will be loaded </div> 

的其他.php簡易裝入使用jquery的文件,簡單易用。