FillForm

Html
Aptilis 2

FillForm(form)

Fillform understands HTML. It will take an HTML form and fill it's fields with the values you want.

If you do a lot of forms, especially when you want to present users with a pre-filled form then FillForm is for you.
Most common example is when you want to get some details from a user and they forgot a compulsory field. So you re-present the form with whatever they already filled in to save a bit of frustration.
FillForm makes this process incredibly easy! It is like Stuff only far more powerful as it understands HTML!

Advantages: It makes code far more legible, it allows non-coding designers to change the look and feel of a site without touching the code, you get more robust applications (in connection with my previous point...), your program is going to be faster.

Inconvenients: None. Seriously.

How it works - once you get that you don't need to read this page again.

Advancing even more

Return value:
the filled in Form in HTML format.

Quick jumps to how tags are handled: text fields
Hidden fields, same as text fields
text areas
radio buttons, check boxes
Drop down lists without 'values', with 'values', with 'multiple'
extra notes on drop down lists

Example 1, let's stay simple:
Usually you'd have your form in a separate file. (For the designer guy to change as his/her creativity demands).
But here for legibility purposes I will keep it all in.

// The \ are only needed because we're in source code.
//In a separate html file, they're not needed.

myForm = "<INPUT Type=\"text\" Name=\"food\">" $


// now we just initialize a variable - note the name!
// It reflects the HTML field name!
food = "pizza" $

// now the black magic! (well)
result = fillForm(myForm$) $

print(result$)
Result:
<input type="text" name="food" value="pizza">
Notice anything? Aptilis took your form and added:
value="pizza"

Now let's pause on this!
Here is how fillForm works:
1. You get your form in a string variable:
   myForm = "<INPUT Type=\"text\" Name=\"food\">" $

2. You set Aptilis variables which names mirror the names of the HTML form elements to the value you want to have inserted. (Only one here in our example) As in:
food = "pizza" $

3. You call fillForm. It will return the form, filled.
result = fillForm(myForm$) $

In effect fillForm will go through your form for each form element (also called field) and see if it has any local variable in the current sub whose name is the same as the element it's looking at.
If it does then your value is going to be inserted in in proper HTML!

Cool hey? Here are some more examples for you to get more excited...

Example 2:
This time we will load the form from a file.

HTML form saved in 'form2.html':

<textarea name="userSays" rows="10" cols="40">this is going to be wiped out</textarea>

Code:

myForm = loadFile("form2.html") $

userSays = "Wow we're typing comments\nin here!" $

result = fillForm(myForm$) $

print(result$)
Result:
<textarea name="userSays" rows=10 cols=40>Wow we're typing comments
in here!</TEXTAREA>

Example 3, radio buttons:
Notice the 'checked' that has been added in the right radio button.
It is important that your radio buttons all have a unique value each within a given set.
Note: radio buttons belong to the same set if they have the same name.

HTML form saved in 'form3.html':

Your Favorite tastes:
<p>
<input type="radio" name="favTaste" value="sweet" /> Candy, fizzy drinks<br />
<input type="radio" name="favTaste" value="salty" /> Meat, French fries<br />
<input type="radio" name="favTaste" value="bitter" /> Guiness<br />

Code:

myForm = loadFile("form3.html") $

favTaste = "salty" $

result = fillForm(myForm$) $

print(result$)
Result:
Your Favorite tastes:

<input type="radio" name="favTaste" value="sweet"> Candy, fizzy drinks<BR> <input type="radio" name="favTaste" value="salty" checked> Meat, French fries<BR> <input type="radio" name="favTaste" value="bitter"> Guiness<BR>

Example 4, drop down list:
We are now going to do a simple drop-down list.
Note that we're not using any value in the 'OPTION' tag. (See next example for that).

HTML form saved in 'form4.html':

Your Favorite colour:
<p>
<select name="favouriteColour">
<option>orange</option>
<option>purple</option>
<option>marine</option>
</select>

Code:

myForm = loadFile("form4.html") $

favouriteColour = "marine" $

result = fillForm(myForm$) $

print(result$)
Result:
Your Favorite colour:
<P>
<select name="favouriteColour">
<option>orange</OPTION>
<option>purple</OPTION>
<option Selected>marine</OPTION>
</SELECT>

Example 5, drop down list again:
this time there are values in the 'OPTION' tags.

HTML form saved in 'form5.html':

Your Favorite colour:
<p>
<select name="favouriteColour">
<option value="1">orange</option>
<option value="2">purple</option>
<option value="3">marine</option>
</select>

Code:

myForm = loadFile("form5.html") $

// also: int(2) $
favouriteColour = "2" $

result = fillForm(myForm$) $

print(result$)
Result:
<P>
<select name="favouriteColour">
<option value="1">orange</OPTION>
<option value="2" Selected>purple</OPTION>
<option value="3">marine</OPTION>
</SELECT>

Example 6, drop down list last:
Sometimes a list can have several of it's options selected at the same time.
(You ususally use [Ctrl]-Click to achieve this)
FillForm understands this too, in a way that is symetric to how you retrieve multiple selections: in an array. Here we're going to select two out of the three options we have.

HTML form saved in 'form5.html':

Languages you know:
<p>
<select name="languages" size="3" multiple="multiple">
<option>English</option>
<option>French</option>
<option>Aptilis</option>
</select>

Code:

myForm = loadFile("form6.html") $

languages[0] = "French" $
languages[1] = "Aptilis" $
// And so on if more are needed. Do it in any order!

result = fillForm(myForm$) $

print(result$)
Result:
Languages you know:
<P>
<select name="languages" size=3 multiple>
<option>English</OPTION>
<option Selected>French</OPTION>
<option Selected>Aptilis</OPTION>
</SELECT>

Notes on Drop down lists: (SELECT)
- in the code that reads: <OPTION>purple</OPTION>
'purple' is called the caption.
- Sometimes an option tag has a value: <OPTION value="24">purple</OPTION>
If an option tag has no value, then the caption is also the value.
- When you assign a Drop-down value you must use the caption if there is no 'value=' thinggy in the tag.
Otherwise make sure you use the value!
- You do not need to use the </OPTION> tag. fillForm is smart enough.
- You can use a mixture of valuefull and valueless OPTIONs in the same drop down list.
- fillForm will do multiple selections, even if you do not have the 'Multiple' flag in your 'Select'. What may happen on the browser if you do that is not certain. Depending on brands and models, the browser may only take one value (the first or the last) or crash, and possibly take your whole computer with it if not your whole LAN and the world. But hey.
- In multiple selects, the order of your array has no importance.

Advancing even more
- You could also use the
stuff predefined sub prior to calling fillform. That would be useful if you don't know in advance what options to put in a drop down list.

See also: stuff.