Tom Green
Tom Green

Reputation: 1

How To Load A PHP Script Using AJAX?

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.

Upvotes: 0

Views: 5839

Answers (3)

The Gr8 Adakron
The Gr8 Adakron

Reputation: 1219

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

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227240

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');
        });
    });
});

Upvotes: 1

FatherStorm
FatherStorm

Reputation: 7183

  $('#web_Content').load('myFile.php');

Upvotes: 8

Related Questions