Home Tutorial Mods Configurator Bookstore Links Email JPDeni

Tutorial -- Create your own forms and displays

Now we're going to start doing some real coding.

(You may have noticed that the I have removed the border from this page. I'm hoping that those with lower resolution monitors will be able to read the whole page without having to scroll from side to side too much.)

We'll start by creating the form. It's a little more complicated than the display, but once you get the form done, the display will be a piece o' cake.

There are a number of ways you can go about creating your form, but I've found this to be the easiest.

You're going to need your field definitions in front of you when you are working on the form. Probably the best thing to do is to print out at least the %db_def part of your default.cfg file. For each field, you will need to know

  • A -- The field name
  • B -- The label you want to have on your form
  • C -- The type of field it is -- text, select, radio, checkbox, textarea or hidden
  • D -- The field length for text fields [or the number of columns (D1) and rows (D2) for text areas]
  • E -- The maximum length of input into the field

It might be a good idea to make some notes on your printout, with the letters that I have assigned to each piece of information for each field. I will be referring to those letters later on.

Open html.pl in your text editor. Find the beginning of sub html_record_form, the first subroutine in the file. You'll see some very informative comment lines, and then the following:

my (%rec) = @_;
($db_auto_generate and print &build_html_record_form(%rec) and return);
	
my $font = 'Font face="Verdana, Arial, Helvetica" Size=2 Color=#003399';

print qq|
<TABLE WIDTH="450" CELLPADDING=0 CELLSPACING=0 BORDER=1 BGCOLOR="#FFFFCC">

Whatever you do, do not remove the first line --

    my (%rec) = @_; 

It is extremely important.

The second line -- the one that starts with

($db_auto_generate and print...

is the command which causes the form to be generated from the .cfg file if you have $db_auto_generate=1; set in the default.cfg file. We're going to be making our own form, so you won't need that line. However, it is better not to delete it. Just "comment it out" -- add a # to the beginning of the line, so Perl will think it is a comment line. (We're leaving $db_auto_generate set to 1 because we won't have the display defined yet.)

The rest of the lines above are more cosmetic in nature, and we can change them later. For now, we're just worrying about functionality.

Leave in the <TABLE ... line, but delete everything below that up to, but not including, the closing </TABLE> tag. Your subroutine should now look like this:

sub html_record_form {
# --------------------------------------------------------
# Several lines of comments

  my (%rec) = @_;
#  ($db_auto_generate and print &build_html_record_form(%rec) and return);

my $font = 'Font face="Verdana, Arial, Helvetica" Size=2 Color=#003399';

  print qq|
  <TABLE WIDTH="450" CELLPADDING=0 CELLSPACING=0 BORDER=1 BGCOLOR="#FFFFCC">

  </TABLE>
  |;
}

Start with any hidden fields you have. You don't really have to put them all together, but it's a little neater if you do.

For each hidden field, copy the code below and paste it just below the <TABLE... tag in the subroutine, substituting the letter codes with the notes on your printout of your default.cfg file.

<input type="hidden" name="A" value="$rec{'A'}">

Next, add all the other fields, in the order you want them listed on your form. Again, copy the code below for each different kind of field and paste it into sub html_record_form, between the <TABLE... and </TABLE> tags, substituting the letters in bold print for the values on your printout from the default.cfg file. Make sure that you enter an input field on your form for every field in your database.

If you have some "admin-only" fields, enter them as if they were visible to everyone. We'll fix them later.

Do not forget that the field names must be spelled exactly as they are in the field definition part of the default.cfg file.

It would be a good idea to keep the formatting as it is here. It will make it easier to see what you're doing later on.

Text fields

<TR><TD ALIGN="Right" VALIGN="TOP"><$font>B:</font></TD>
    <TD VALIGN="TOP">&nbsp;
      <input type="text" name="A" value="$rec{'A'}" size="D" maxlength="E"></TD></TR>

Select fields

<TR><TD ALIGN="Right" VALIGN="TOP"><$font>B:</font></TD>
    <TD VALIGN="TOP">&nbsp;|;
      print &build_select_field("A","$rec{'A'}"); print qq|</TD></TR>

Radio fields

<TR><TD ALIGN="Right" VALIGN="TOP"><$font>B:</font></TD>
    <TD VALIGN="TOP">&nbsp;|;
      print &build_radio_field("A","$rec{'A'}"); print qq|</TD></TR>

Checkbox fields

<TR><TD ALIGN="Right" VALIGN="TOP"><$font>B:</font></TD>
    <TD VALIGN="TOP">&nbsp;|;
      print &build_checkbox_field("A","$rec{'A'}"); print qq|</TD></TR>

Textareas

<TR><TD ALIGN="Right" VALIGN="TOP"><$font>B:</font></TD>
    <TD VALIGN="TOP">&nbsp;
      <TEXTAREA NAME="A" ROWS="D2" COLS="D1" WRAP="VIRTUAL" 
          MAXLENGTH="E">$rec{'A'}</TEXTAREA></TD></TR>

Once you get all of your fields entered, save and upload your html.pl file to your server. (Don't change the $db_auto_generate setting in your default.cfg file yet.) Add a record or two, search for records, modify and delete a couple. Make sure you don't come up with any error messages, either from your server or from DBMan.

If you get server errors, first check to make sure you uploaded your file in ASCII mode. At this point, if you didn't upload in ASCII mode, you will probably get a CGI error mentioning "stripping carriage returns after upload." If you get that message, you uploaded in binary mode. You will probably have to delete the file and upload it again, and also set the permissions again.

If you copied the code from this page for each of your fields, only substituting the letters for the correct values, and you pasted the codes within the table tags, you shouldn't get any other kinds of errors.

If you get errors within DBMan, such as no records returned when you know there are some that match your search terms or modify or delete failures, you probably have typed the name of one of your fields incorrectly. If the value of a field is not filled in when you modify a record, that also is probably due to a mistyped field name.

Whatever you do, do not go on with the tutorial until you find out what the problem is and you fix it. Problems will not go away and they will get worse as you do more scripting.

Now that we know your fields are entered correctly, we can do some more stuff with the form. The first thing we need to do is set up any "admin-only" fields.

I think the best way to explain the procedure is to give you an example. Below is an extremely small form for a database:

print qq|
<table>
  <tr><td>Name:</td>
      <td><input type="text" name="Name" value="$rec{'Name'}"></td></tr>
  <tr><td>Userid:</td>
      <td><input type="text" name="Userid" value="$rec{'Userid'}"></td></tr>
  <tr><td>Address:</td>
      <td><input type="text" name="Address" value="$rec{'Address'}"></td></tr>
  <tr><td>Gender:</td>
      <td>&nbsp;|; 
          print &build_radio_field("Gender","$rec{'Gender'}"); print qq|</td></tr>
  <tr><td>Favorite Color:</td>
      <td>&nbsp;|; 
          print &build_select_field("Color","$rec{'Color'}"); print qq|</td></tr>
</table>
|;

Let's say that you would like the Userid and Gender fields to be "admin-only." You would make the changes noted below in bold print:

print qq|
<table>
  <tr><td>Name:</td>
      <td><input type="text" name="Name" value="$rec{'Name'}"></td></tr>|;
if ($per_admin) {
 print qq|
  <tr><td>Userid:</td>
      <td><input type="text" name="Userid" value="$rec{'Userid'}"></td></tr>|;
}
else {
 print qq|<input type="hidden" name="Userid">|;
}
print qq|
  <tr><td>Address:</td>
      <td><input type="text" name="Address" value="$rec{'Address'}"></td></tr>|;
if ($per_admin) {
 print qq|
  <tr><td>Gender:</td>
      <td>&nbsp;|; 
          print &build_radio_field("Gender","$rec{'Gender'}"); print qq|</td></tr>|;
}
else {
 print qq|<input type="hidden" name="Gender">|;
}
print qq|
  <tr><td>Favorite Color:</td>
      <td>&nbsp;|; 
          print &build_select_field("Color","$rec{'Color'}"); print qq|</td></tr>
</table>
|;

If you had any "admin-only" fields and made changes to your form, again upload the script and test it out. Log on with both admin and non-admin permissions -- if you haven't changed the default.pass file, log on first as admin/admin and then as author/author. Make sure you can see the fields when you are logged in with admin privileges and can't see them when you aren't.

If you get 500 errors, you probably either left off an opening or closing curly brace -- { } -- or forgot to close off a print qq| statement with a |; or used parentheses -- ( ) and curly braces in the wrong places. Sometimes it's hard to tell the difference between them.

And now for the display part, which I call JPDeni's Infamous Delete-Copy-Paste-Delete method of creating a record display. You'll see what I mean.

Open your html.pl file in your text editor. Locate sub html_record, which is just below sub html_record_form. The first step is to delete everything, starting with the line

    <TABLE WIDTH="475" CELLPADDING=0 CELLSPACING=0 BORDER=1 BGCOLOR="#FFFFCC">

all the way down to, and including

    </TABLE>

Be sure to leave the print qq| at the beginning and the |; at the end of the subroutine.

Now scroll up the script to the beginning of sub html_record_form. Copy the whole table, starting with the

    <TABLE ...

tag, all the way down to, and including

    </TABLE>

Scroll back down to sub html_record and paste the table between print qq| and |;.

Now it's time to delete the stuff you don't want from the display. (I don't know why, but it always give me pleasure to delete stuff. <Grin>)

First, delete all the <input type="hidden"... fields.

Next, if you have added any code for "admin-only" fields, delete the

    else {
      print qq|<input type="hidden" ...|;
    }

parts of the code. Leave the if ($per_admin) { parts, though, if you want only those with admin permissions to see the values of these fields.

You will notice that every one of your input fields has a structure somewhere in it that looks like $rec{'FieldName'}. In every row, delete everything except the table formatting and the $rec{'FieldName'} structure. Using my previous example, your resulting display will look like this:

<table>
  <tr><td>Name:</td>
      <td>$rec{'Name'}</td></tr>|;
if ($per_admin) {
 print qq|
  <tr><td>Userid:</td>
      <td>$rec{'Userid'}</td></tr>|;
}
print qq|
  <tr><td>Address:</td>
      <td>$rec{'Address'}</td></tr>|;
if ($per_admin) {
 print qq|
  <tr><td>Gender:</td>
      <td>$rec{'Gender'}</td></tr>|;
}
print qq|
  <tr><td>Favorite Color:</td>
      <td>$rec{'Color'}</td></tr>
</table>

Save your html.pl file and open your default.cfg file. Set $db_auto_generate = 0. Save default.cfg and upload both default.cfg and html.pl to your server. Check it out. Things will probably not be exactly as you want them yet, but you can see if the display actually displays the information.

  The basic tools

Some basics of Perl

  Install the demo

Create your own forms and displays

  Create your own configuration

Fancier formatting

  Set permissions

How to install a mod

If you need help

Home Tutorial Mods Configurator Bookstore Links Email JPDeni

Text-only site map

Background by Windy