Writing Functions to Build Forms

In the past, I had usually thought of “functions” in programming as quick, simple little bits of code that would perform simple tasks over and over again for you. Now, I think on a grander scale.

In addition to writing functions to perform various tasks like validating an e-mail address, reformatting text, running a regexp, checking permissions, etc., you really can use functions to do just about anything you want to repeat.


The most recent conclusion I have come to is that functions are extremely useful for building forms. If you write scripts in which you need to duplicate forms over and over again, you may have simply used for…each or do…while loops to accomplish this. That’s fine, as long as you only ever use one file to generate that form.

However, if you need a “user input” form and an “admin edit” form, then you may run into an issue if you use that method. Sure, you could write the same form twice, but that can get tedious if you decide to add an item to the form, remove an item from the form or simply change an item that’s on the form.

That’s where functions come in handy. You can use a function to build your form and place that function inside an include file. Then, you can simply include the file in your “user input” page and your “admin edit” page. When you need to change the form in some way, you can simply change it in that one place, and that change will take effect on all of your pages.

This can become especially useful when you are ready to duplicate the form. Say you want to allow the “admin” to edit multiple user entries at once. If you use a function to create the form, you can simply add a unique key into the function that will allow you to easily duplicate the form.

Here is an example of how I build my forms in VBScript:
<%
function buildForm(idnum)
%>
<label for="myText1_<%=idnum%>"><%=formLabels("myText1")%></label>
<input type="text" name="myText1_<%=idnum%>" id="myText1_<%=idnum%>" value="<%=results("myText1_" & idnum)%>" />
<label for="myText2_<%=idnum%>"><%=formLabels("myText1")%></label>
<input type="text" name="myText2_<%=idnum%>" id="myText2_<%=idnum%>" value="<%=results("myText2_" & idnum)%>" />
<%
end function
%>

You can do the same thing in PHP pretty easily. In the example above, I’ve created a scripting dictionary called “formLabels” in which I’ve stored my labels (it’s not really necessary at this stage in the game, but it can be helpful down the road), a scripting dictionary called “results”, in which I can store the results of any entries that may have already been processed.

If you were doing this in PHP, you would use associative arrays, which are essentially the same thing as the scripting dictionaries in VBScript.

When you’re ready to create a form, you simply call the function. If you are pulling multiple ID’s, you can run the function through a for…each or do…while loop.

It could look something like:
<form name="myForm" id="myForm" action="<%=frmAction%>" method="post">
<%
For each myKey in myIDs
Response.Write(buildForm(myKey))
Next
%>
</form>

Sure, for the example I’ve shown above, this function may seem a little silly. However, when you start building forms with 10 or 20 or even more inputs, it can get a little tedious to repeat the information over and over again. In addition, if you find yourself building forms over and over again, you can easily duplicate your files without having to really edit more than a few lines of code.

In the next installment, I’ll show you how you can build a function to process the form extremely quickly and easily. If anyone wants me to write out my examples in PHP, I’d be glad to do so. Just let me know.

Tech Tags: HTMLCenter

Did you like this post? Get monthly summary of our new tutorials, posts and tips to your inbox!

No Responses

Post Your Comment

Your email address will not be published.