最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How To Load A PHP Script Using AJAX? - Stack Overflow

programmeradmin0浏览0评论

I would like to know the best technique for loading a PHP page and insert into a section of a page using AJAX?

For example, consider the following HTML code:

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

What would be the best way to load a page (help.php or contact.php for example) using AJAX into the contents Div section?

I am using the JQuery library for my site if that helps.

I would like to know the best technique for loading a PHP page and insert into a section of a page using AJAX?

For example, consider the following HTML code:

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

What would be the best way to load a page (help.php or contact.php for example) using AJAX into the contents Div section?

I am using the JQuery library for my site if that helps.

Share Improve this question asked Apr 8, 2011 at 18:37 Tom GreenTom Green 11 silver badge1 bronze badge 1
  • 1 Please... I beg you... don't do your entire site like this with Javascript. Simple server-side includes are easy with PHP. – Brad Commented Apr 8, 2011 at 18:42
Add a ment  | 

3 Answers 3

Reset to default 8
  $('#web_Content').load('myFile.php');

I assume you want the page to load in the web_Content div when a link is clicked, If so, you can do something like this:

$(function(){
     $('#web_Menu a').click(function(e){
        e.preventDefault();
        $('#web_Content').load($(this).attr('href'), function(){
            alert('File loaded');
        });
    });
});
<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>

Easy loading of other .php file using Jquery, simple and easy.

发布评论

评论列表(0)

  1. 暂无评论