Webmaster Forums 101 - Free Help and Discussions
 
 
 

MEMBER REWARDS - Earn free advertising by participating in these forums! Click here for details.

Welcome to Webmaster Forums 101!

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

Reply
 
Thread Tools Search this Thread Display Modes
Old 21st October 2005, 09:21 AM   #1
Mr. Tech's Avatar

Username: Mr. Tech
Rank: Problem Solver
Join Date: Dec 2004
Location: Gold Coast, Australia
Posts: 2,166
Points: 1,025.61
Send a message via MSN to Mr. Tech
Default AJAX Tutorial - Including Files

I've have been playing around with AJAX this past week... I had a client who needed a script that includes the contents of a PHP file using AJAX... I'm far from an AJAX expert but all I know is that this script works. Hopefully all the coding is correct

The main file:
Code:
<html> <head> <script language="javascript" type="text/javascript"> function createRequestObject() { var ro; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer"){ ro = new ActiveXObject("Microsoft.XMLHTTP"); }else{ ro = new XMLHttpRequest(); } return ro; } var http = createRequestObject(); function sndReq(page) { http.open('get', 'load.php?page='+page); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4){ var response = http.responseText; document.getElementById('content').innerHTML = response; } } </script> </head> <body> <p><a href="javascript:sndReq('page1')">Load Page 1</a> : <a href="javascript:sndReq('page2')">Load Page 2</a> : <a href="javascript:sndReq('page3')">Load Page 3</a></p> <div id="content"> This is the default content. </div> </body> </html>

Basically when you click one of the javascript:sndReq('pagename'), it will load whatever you put inside the inverted commas onto the page. So in this case it would load pagename.php. All loaded text is placed into the content div.

In the sndReq() function, it opens load.php which checks that the page exists. If the page does exist, it will load the page. If not, it will give an error message.

The load.php file:
PHP Code:
<?php
if ($_GET['page'] && file_exists($_GET['page'].".php")) {
    include(
$_GET['page'].".php");
} else {
    echo 
"This page does not exist!";
?>

The PHP file is very basic. It checks that the file exists and then includes it.

Now all you have to do is create the .php files.

Hope that helps
__________________
Please read the Forum Rules

Get rewarded for posting
- free advertising!

.::. [ Webmaster Resources ] .::. [ Web Design ] .::. [ Developer Tutorials ] .::.
Mr. Tech is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Login
User Name
Password


Featured Members


Featured Links


Useful Resources
Webmaster Resources
Developers Tools
Developer Tutorials


Partners

All times are GMT +10. The time now is 12:42 PM.


Powered by vBulletin Version 3.5.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.3.2
 
 
WEBMASTER FORUMS 101
ARCHIVE
CONTACT US
TOP
   
© Webmaster Forums 101 2005. All Rights Reserved.
Design by: vBCore