|
|
 |
Lesson 14: EZ update
Summary:
Learn to use VBScript and text files to make content on your site easy to update
(in fact so easy that your boss could do it!). All code is available.
View the demo to see the result.
Intro:
Most of today's websites features content that needs to be fresh, like
news, pricelists, special offers etc. etc.
And usually you have to go through this long routine everytime you need to make an update:
1. Open the HTML/ASP file with your texteditor/HTMLeditor.
2. Make the changes.
3. Upload the file to your website through an FTP program
This takes some time, but it's not that bad. The only real problem is
that YOU have to do the update. Would you let your co-workers who
can't even spell HTML anywhere near that file? I think not!
So what can you do? As always haneng.com comes to the rescue!
The theory:
We are going to make an ASP page that pull some of it's
content from a text file. And we are going to ad a simple
form where you can edit the content of that text file.
It's a simple solution for webpages that doesn't have the
luxury of a database.
You can use this script for a number of functions
(news, pricelist, special offers etc.), and we
are going to pretend that we are making a site
for the used car salesman "Sleezbag" Bob.
Every week Bob has got a special offer on a
crappy old car. Now he will be able to change
the special offer each week himself.
The code:
We will use 4 files:
- Default.asp This file is the main page for Bob's used cars
- Edit.asp The file Bob uses to change the special offer
- Save.asp The file used to save the changes to the text file
- Content.txt The text file where we store the car of the week
I'm not going to show the whole code here, but
I will explain some parts of the code that may
be difficult to understand:
In default.asp we use this line to get the info from the
file (this line is looped until the end of the file.):
Response.Write(MyTextFile.ReadLine & "<BR>")
But in Edit.asp we use this:
Response.Write(MyTextFile.ReadLine & Chr(10))
Why the <BR> and Chr(10)? Well in Default.asp we
want the code to be displayed with the same linebreaking
as we make in the edit form. The Chr(10) does the same
in our editform. If we didn't use Chr(10), then all
the text in the text file would be displayed in one long line.
In the Save.asp we use the parameters 2 and TRUE:
Set MyOutStream=MyFileObj.OpenTextFile(Server.MapPath("content.txt"), 2, TRUE)
As you may remember from the Guestbook lesson
we used 8 instead of 2. So what does it mean?
Well 8 means that everything we write to the file
shall be placed under the current content of the file.
We want the old content overwritten with the new, so therefor
we use 2. TRUE means that if the file doesn't exist, it should
be created for us.
You may also see that I use Response.Write()
and not <%= %>. The result is actually the same,
and if you want, you can write it like this:
%><%=MyTextFile.ReadLine%><BR><%
Try the demo of Bob's used cars page (HTML tags are disabled in this
demo)
Click here to download all the code.
Troubleshooting
- Make sure that you have write access for the Content.txt.
That often means that you have to place the file in the
cgi-bin directory on your web server.
- You may get problems if you use " in the text. In my tests I have had no problems, but
if you modify the script, you may get some. Other characters that may cause
trouble are < and >. The solution is using the Replace()
function. For more info on that, see lesson 13.
Modifications
You should make some kind of protection for the Edit.asp
page to prevent people from altering your site.
Use the Bouncer or the Password script from our Code Archive
to do this.
You can also ad some simple formatting with the Format_text script found on the same page.
A last comment:
I hope you find this script useful and it shouldn't be too hard
to implement into your own site.
Where to go next:
Take a look at our lessons page
to get an overview of all our lessons.
| |
|
|
 |
|