Jun 9, 2009

Uploading files to your webpage

Have you ever wanted to upload your files to your hosting, or have you ever want to make a simple images gallery and let the visitors upload their pictures.



In this tutorial I will show you how simple is to write a script for uploading your files online with no need for complicated web scripts like ASP or PHP.



Simple HTML uploading form


If you want to upload images you will need a form, it is made in pure HTML, it is the page where you click on Browse and select the file you want to upload. Fortunately HTML provides us with the elements, we do not need to write scripts to list files on our computer.



<html>

<head>

<title>Upload an Image</title>

</head>

<body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form action="upload.wbsp" method="post" ENCTYPE="multipart/form-data">

Select file (*.jpg;*.gif - max. 100KB): <input type="file" name="image" size="20"> <input type="submit" name="sButt" value="Upload">

</form>

</body>

</html>


Let take a look in some important elements in this code, first the Form tag, without it our code will not work, the form tag must contain information like action (where to send the file data), method (there is two general methods – get and post, when sending files use post) and enctype(there is a lot of encoding types, multipart/form-data is used for uploading files).


Also we need to use the input of type file, that input is the one which let us browse our computer files. Last is the submit input, it is the button which people will click to send or upload the file.



Save this file as index.htm and put it in a folder so you do not loose it.



The butter of the work


Now we want to make the server-side file, it is the script which takes the file, save it on the hosting server.


[FormFields]

WB_AllowMultipart=T



[Upload]

WB_Disallow=![jpg,gif,png,bmp]

WB_UploadDir=/

WB_Overwrite=T

WB_MaxFSize=102400



<!--WB_BeginTemplate-->

<html>

<head>

<title&rt;File uploaded</title>

</head>

<body bgcolor="#ffffff" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

Your file have been uploaded!

</body>

</html>


OK, do not get afraid, it is not complicated as it seems, I will explain every line before the HTML tag, make your self a coffee or a tea and sit down and read.



I will explain first what is used as a scripting language, it is WhizBase Server Pages, WhizBase is simple but powerful scripting language, it is made for non-programmers to simplify creating database-driven websites without the need for high experience in high-level programming languages.



Now in English, it is a scripting language for everyone, simple and easy for you to use. Let me show you how.



The header section


Every WBSP page have a header, it is a place where we put some information needed by the server, everything we write in this section will not appear in our page. This section contains the variables that are essential for processing WBSP file. Here you put information about the database, recordset, template, error template, log file, redirection, etc.



In the code we have [FormFields] which is a tag giving a notice for WhizBase engine to start interpreting the main commands of the WBSP file.



Then we have WB_AllowMultipart = T this variable controls if the current WBSP page will accept uploaded files (sent by client using multipart form). If this variable is set to TRUE WhizBase will accept and process uploaded files. This is a security measure so WBSP process the files sent to the write direction.



Second tag is [Upload] which is giving a notice for WBSP engine to start receiving information about the uploading process. Now ask your self, do you want viruses and a porn dialer on your server, if no you need to use WB_Disallow=![jpg,gif,png,bmp], we do not want users to upload any file, we only want images, so we disallow every file which does not have one of these extensions.



Where do you want to put your pictures, you need an upload directory, so we use WB_UploadDir=/, this variable defines the name of the directory on the server where WhizBase will save files uploaded using current WBSP file.



Do you want to overwrite your image? WB_Overwrite=T is a variable which defines if the file with same name that already exists on the server will be overwritten by newly uploaded file. We will use T as True. If you define it as F (False) WhizBase will generate a unique file name for the new one and save it like that.



If you do not want visitors to block your server, you need to limit their file's upload size, so you use WB_MaxFSize=102400 which is a variable which defines the maximum size (in bytes) of a single file that can be uploaded using current WBSP file. We have putted 102400 bytes.



Finally we put <!--WB_BeginTemplate--> to let the server know that now we are starting the body section, where we put our HTML code and what we want to show for visitors.



As you see, we can control everything when uploading files. And that how you simply make an upload form which works without the need of PHP or ASP.



For more information about WhizBase please visit WhizBase site

Jun 3, 2009

Make a database driven website in 3 steps

Today every company had to have a website, it is something like having a telephone number or a company address. A lot of companies do not have yet a budget for making a website, they think it is very expensive, and they are right. If you use a web developer in Europe you will need a couple of thousands of Euros to make a simple website to present your company.


In this article I will give a simple tutorial how to publish your database report without the need for a web-developer, something simple but yet very powerful, with your Microsoft access database.


This method is best way for web designers, who know how to make a web page in HTML, but do not know how to connect it with the database, without using PHP or ASP.


First step: create your report page


To show your report online, you will need a database access file and a HTML file which will show the report or the query results.


We will create the databse in Microsoft access, I will create a database and name it as biblio and create a table and name it Titles, we will make these fields:


  • ID as number data type which will be our primary key also.

  • Name as text data type

  • Publisher as text data type

  • PublishYear as text data type

I will fill it with some data and save it, and we have a database file.


If you have a design for your report you will need to slice it and make a HTML page, you can use GIMP on linux or Photoshop on windows, then use any HTML editor or text editor to make the HTML code.


I will use a simple example using WhizBase Server Pages (WBSP) to develop this report. WBSP is a very powerful tool for publishing databases online with a very simple code, it is not like classic web programing languages.


Create the header:


Every WBSP page have a header, it is a place where we put some information needed by the server, everything we write in this section will not apear in our page. This section contains the variables that are essential for processing WBSP file. Here you put information about the database, recordset, template, error template, log file, redirection, etc. We will simply say for the server to connect to our access database and select a table, list for example 10 records only and make a pagiation.



1 [FormFields]

2 wb_basename=biblio.mdb

3 wb_rcdset=Titles

4 WB_Command=Q

5 WB_MaxRec=10

6 <!--WB_BeginTemplate-->

[FormFields] is the starting tag for the section, when the server sees this code it will start receiving our commands. wb_basename=biblio.mdb is our database file, I have putted our database in the same folder as my HTML file so I am calling it directly. The server will look for the file name what ever we give as a path for it after wb_basename and connect to the database file. To specify which table we will select we use wb_rcdset=Titles, as you see I will select the table Titles. We told the server which database file to connect and which table to select, now we need to tell it what to do, and in our case is query, using the command WB_Command we give it a value Q and we did it. Finally we want to limit our results, let us show 10 records by page. We can skip this line and it will list the whole table, but what if we have a table with 10 000 records or more, do you really want to show it all in one page? So we will use WB_MaxRec=10 and that is all what we need. Now give the server a simple comment like command <!--WB_BeginTemplate--> which says to the server begin interpreting the template.


Create the body (template):


After creating the header setion we have to create the template, and that is very simple, it is our HTML code with simple lines of WBSP code where we want to show our data.



<html>

<head>

<title>Simple DB report page</title>

</head>

<body>

<table>

<tr><th>ID</th><th>Name</th><th>Publisher</th><th>Publish Year</th></tr>

<!--WB_BeginDetail-->

<tr><td>$wbf[ID]</td><td>$wbf[Name]</td><td>$wbf[Publisher]</td><td>$wbf[PublishYear]</td></tr>

<!--WB_EndDetail-->

</table>

<div align='center'>$wbnavigator</div>

</body>

</html>


In the template we want to view ten records in a table and then show the navigation bar where users can go next or previus page to see more records.


The most important code in this template is <!--WB_BeginDetail--> and <!--WB_EndDetail--> which represents the start and the end of the looping function, everything between these two will loop for as many times as records we want to show. If the query returned 10 records it will loop for ten times. Between these two commands we will show the records using the function $wbf[fieldname], in our case we are viewing four fields in the table and WBSP will replace every one with the field value in the table.



Finally we have $wbnavigator which will generate for us the navigation links automatically, this is a very cool command, we do not need to calculate anything, it will automatically create as many pages as we need.



We will save all this as defaut.wbsp file, where the extension wbsp will say to the server that this file have a WBSP code in it.



Second step: Create a search form


In the previous step we have made a report page, now we need a filtering form, for example what if we do not want to view all the records, we want to view just records for the publish year 2007.



Create a file named as «search.htm», in this file we will not need any WBSP code, we will only make a HTML form with inputs and a submit. We only must take into account one thing, how we will name our inputs, because WBSP have its automatic mechanism for catching get and post values.




<html>

<head>

<title>Search filter file</title>

</head>

<body>

<form action='default.wbsp' method='post'>

ID <input type='text' name='wbf_id' /><br />

Name <input type='text' name='wbf_name' /><br />

Publisher <input type='text' name='wbf_publisher' /><br />

Publish Year <input type='text' name='wbf_publishyear' /><br />

<input type='submit' value='submit' />

</form>

</body>

</html>



As you can see we have used a pure HTML, but we have also used a specific naming method in the input names. WBF_ID is WBF_ + ID where WBF_ is WhizBase prefix and ID is the name of the table field. WhizBase catches all the get and post data and filters them, then takes all the post and get data with prefix WBF_ and sends them to the query.



For example if we put a value 2000 in «Publisher Year» input, it will be a post value in the variable wbf_publisheryear, WhizBase will filter it and catch it because it has wbf_ prefix and then send it as a database query as «publisheryear = 2000».



Third Step: Upload everything online


Finally, we have a form file, a report file and a database. We do not need anything else. Now just upload the 3 files online in the same folder on a server which supports Whizbase or install Whizbase on your Windows server and put these files on the server and you will have a ready report and a filtering system.



For more information about WhizBase or to download it for free you can visit http://www.whizbase.com