Home      Products & Services      Contact Us      Links

WebHatchers will design & develop your site for you.

Website Menu Heaven: menus, buttons, etc.

Send us your questions.

site search by freefind

SEO, Google, Privacy
   and Anonymity
Browser Insanity
Popups and Tooltips
Free Website Search
HTML Form Creator
Buttons and Menus
Image Uploading
Website Poll
IM and Texting
   or Not MySQL
Personal Status Boards
Content Management
Article Content
   Management Systems
Website Directory
   CMS Systems
Photo Gallery CMS
Forum CMS
Blog CMS
Customer Records
   Management CMS
Address Book CMS
Private Messaging CMS
Chat Room CMS
JavaScript Charts
   and Graphs

Free Personal Status Boards (PSB™)

Free Standard Free PSB

Free PSB Pro Version

Free Social PSB

Free Social PSB Plus (with Email)

Free Business PSB

Free Business PSB Plus (with Email)

PSB demo

Social PSB demo

Business PSB demo

So what's all this PSB stuff about?

Chart comparing business status boards

PSB hosting diagram

PSB Licence Agreement

Copyright © 2002 -
MCS Investments, Inc. sitemap

PSBs, social networking, social evolution, microcommunities, personal status boards
PSBs, social networking, business personal status boards
website design, ecommerce solutions
website menus, buttons, image rotators
Ez-Architect, home design software
the magic carpet and the cement wall, children's adventure book
the squirrel valley railroad, model railroad videos, model train dvds
the deep rock railroad, model railroad videos, model train dvds

PHP Script for Writing AJAX Web Page Files

PHP can write just about anything. The script below will concentrate on AJAX website pages.


The one thing you need to watch here is permissions. On some servers, you need permissions (CHMOD) set to 755 to use the PHP fwrite function, which all the file creations below use in their code. But on other servers (like ours, we discovered), you need permissions set to 777. There are people that understand why. We are not those people, nor is our server host. Older servers seem to need 777. Newer servers need only 755—which is obviously more secure. But this is not a hard and fast rule—your mileage may vary. If you find that you need 777, you may find your host blocks this setting. But you may find you can fwrite with only a 755 CHMOD (public_html AND the PHP script file setting). Some hosts disallow fwrite and even PHP file functions in general. You will find that write-nearly-any-type-of-file-with-php.html is the best place you can go for answers if you are testing a PHP script for writing a file and you get the dreaded permission error message.

Writing Various Types of Files

There are times when you just need to use PHP to write various types of files. The list below shows you how—it's not hard at all. There are even five PHP scripts for creating graphics/image files. For image creation, we concentrated on the simple task of allowing users to input text and using PHP to take this text and put it in a nice box with a border and save it as a graphics/image file. Let us know how you like these scripts! We kept them as simple as we could so you could get the idea in a flash.

The PHP Script for Writing AJAX Web Page Files

First, notice that we escaped all the double quotes inside the $p string so as not to conflict with the double quotes the $p string is surrounded by. And of course, the newline characters are escaped (\n). In the script, we will have a button with the words "Change Text" on it and the word "Change!" above it. We also have a text file called ajax.txt which contains only the phrase "See, it changed!" The simple script lets users push the button and the word "Change!" will change to the phrase "See, it changed!" because the Ajax script will read the text file, grab the contents, and use them as a replacement for the word "Change!" on our web page. Cooler yet, the page will not have to do any reloading in the process of going and getting text from a separate online page and using it to edit the current page's content. True, the same trick could be performed with a simple JavaScript function without page reload and without recourse to an exterior page. But it would work only if the current web page had prior knowledge of what the needed change was. What if it didn't have a clue what the change was to be and only the ajax.txt page contained that info? For that matter, what if the info was only to be found in an online MySQL database table which was being updated and read by Ajax scripts every 8 seconds? That is the exact scenario in our Ajax chat room script.

Anyway, the current script will write an HTML page featuring the resplendant glories of Ajax, which uses JavaScript and XMLHTTP. But first, let's check out the PHP aspects of the script. The $p variable is most of the script (22 lines long!). After the closing tag for HTML is a " character. That is the end of the PHP string with the variable name $p. Next we use fopen and put in a file name and a w for write. Then we use fwrite to write the file. Next we use fclose, which you should always do when using PHP file functions. Finally we CHMOD the permissions to a nice safe 644. A created file may be getting this CHMOD by default anyway, but why take chances? Of course, your host may not allow CHMOD from a PHP file anyway. Some do; others don't.

So that takes care of the PHP, which simply writes an HTML file whose contents are what's in the PHP variable string $p. The rest of the script is standard HTML stuff and then a nice big JavaScript function in the head section between script tags. (If you'd like to explore Ajax further, try Ajax—there are some great tutorials there.)

What is Ajax? AJAX means Asynchronous JavaScript and XML, but it isn't a programming language. It is a new way to use existing languages. When one uses Ajax, one is exchanging data with a server and updating parts of a web page, but without reloading the whole page! For a great example of Ajax in action, see http://www.w3schools.com/Ajax/ajax_aspphp.asp or http://www.css-resources.com/Ajax-and-PHP-Based-Insult-Auto-Completer.html. Happily, one need not learn XML or even XHTML to use Ajax, although there's a whole ton one can do if one does learn those. But those are not of interest to us currently so let's stay with the problem at hand. Incidentally, Ajax can be on regular HTML or PHP pages—you need not go to XML or XHTML.

On to the code: Notice the div with the id "myCoolDiv". This is where the "Change!" word will be. When the Ajax routine runs, using XML to consult the text file, it gets back its xmlhttp.responseText from this text file, and it stores it in the innerHTML of the div with the id "myCoolDiv". (To see the script without all the annoying backslashes, click on the link below, run the script, and view the source code.) Once the text file text is stored in our web page's div, the user now sees this text, and the original page text is gone forever unless you refresh the page with F5. If you want more info on what the xmlhttp stuff is, check out Ajax.

Here is the result: my_ajax_file_written_with_php.html

<title>my ajax file written with php</title>\n
<script type=\"text/javascript\">\n
function loadit(){\n
if (window.XMLHttpRequest)\n
{xmlhttp=new XMLHttpRequest();}\n
{xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");}\n
if (xmlhttp.readyState==4 && xmlhttp.status==200)\n
<div id=\"myCoolDiv\"><h1>Change!</h1></div>\n
<button type=\"button\" onclick=\"loadit()\">Change Text</button>\n
$a = fopen("my_ajax_file_written_with_php.html", 'w');
fwrite($a, $p);
chmod("my_ajax_file_written_with_php.html", 0644);