اول توی html اصلی یک div درست کنید مثلا با آی دی changable-div
کد:
<!-- some html here -->
<div id='changable-div' >
<!-- leave empty for now -->
</div>
سپس توی جاوا اسکریپت فانکشن بسازین:
کد:
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest(); //Not IE
} else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP"); //IE
} else {
//Display your error message here.
//and inform the user they might want to upgrade
//their browser.
alert("Your browser doesn't support the XmlHttpRequest object. Better upgrade to Firefox.");
}
}
که تولید کننده درخواست آژاکس در همهی مرورگر ها هست. سپس یک فانکشن دیگه بسازین و وصل اش کنین به یه دکمهای، چیزی:
کد:
//Get our browser specific XmlHttpRequest object.
var receiveReq = getXmlHttpRequestObject();Next we will write our function that is called when a user clicks our hyperlink. This function will initiate the asycronous call.
//Initiate the asyncronous request.
function sayHello() {
//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
//Setup the connection as a GET call to SayHello.html.
//True explicity sets the request to asyncronous (default).
receiveReq.open("GET", 'YOUR_PHP_SCRIPT_HERE.php', true);
//Set the function that will be called when the XmlHttpRequest objects state changes.
receiveReq.onreadystatechange = handleSayHello;
//Make the actual request.
receiveReq.send(null);
}
}
و تموم!