Biography:

Location: Dayton, OH
College: Cedarville University, Cedarville, OH
Degree: B.A. Management Information Systems (2002)
Certifications: Microsoft Certified Professional (MCP) 2002

Interests:

Music, Playing Guitar, Working on Cars, Home Improvement, Photography, Computers, Website Programming, Driving in the snow, NHL Hockey, Sand Volleyball, Settlers of Catan board game

Favorites:

Foods: Lasagna, Carrot Casserole, Chicken Parmesan
Sports: Hockey, Football, Volleyball, Soccer
Desserts: Peanut Butter Pie, Peanut Butter Passion ice cream
TV Shows: 24, The Office, Heroes, Mythbusters, Top Gear, Fifth Gear, Modern Marvels, Seinfeld, Simpsons


Contact Me:
Use this area to send me a note or message:
Name:
Email:
Comments:
Security Code:

Fun links you should probably check out:


Fun games you might want to play:


Current Code Samples:


Future Code Samples:

  • CAPTCHA form-protection of images using PHP and GD 2.0 library
  • Resizing images using PHP and the GD 2.0 library
  • Rotating images using PHP and the GD 2.0 library
CODE TUTORIAL: Using HTML form input arrays with ColdFusion 7
After searching around for countless hours, trying some test code samples, and even talking to the ColdFusion master himself (Ben Forta), I have come to find that it is IMPOSSIBLE to use HTML input form arrays with ColdFusion. If you've ever run into problems trying to get ColdFusion to recognize similar input items in a collection, there is a solution at the bottom of this page.

I'm a tried-and-true PHP/C#.NET programmer and am very comfortable with the C-based language syntax structure. And in previous web projects with other companies I've worked for, I was easily able to create HTML form input arrays out of whatever type of elements I wanted to use, whether TEXTAREA, INPUT, SELECT.

Here is a code snippet showing how I would create the HTML form input arrays:
(take notice of the empty value for the 3rd textbox item, this will illustrate my point later on)
HTML CODE: "input-form.html"
<!--  HTML Input Array Form -->

<form name="FirstNameInputForm" method="post" action="receive-form.php">

  <input type="text" name="First_Name[]" size="20" maxlength="30" value="John">
  <input type="text" name="First_Name[]" size="20" maxlength="30" value="Jack">
  <input type="text" name="First_Name[]" size="20" maxlength="30" value="">
  <input type="text" name="First_Name[]" size="20" maxlength="30" value="Sam">
  <input type="text" name="First_Name[]" size="20" maxlength="30" value="Mike">
  <input type="text" name="First_Name[]" size="20" maxlength="30" value="Mark">
  
  <input type="submit" name="submit" value="Submit Form">
  
</form>
Here is the "code behind" page in PHP that I'd use to receive and loop through those FORM post values:
PHP CODE: "receive-form.php"
<?
  //  Retrieve input array form values
  $First_Name_str = $_REQUEST["First_Name[]"];      //  Capture REQUEST variable into string variable
  $First_Name_arr = explode(",", $First_Name_str);  //  Split the comma-separated string into a PHP array

  //  Loop through form values
  for($i=0; $i<count($First_Name_arr); $i++)
  {
    echo $First_Name_arr[$i]."<br>";  //  Output each name on a new line
  }
?>
Here is the "code behind" page in C#.NET that I'd use to receive and loop through those FORM post values:
C#.NET CODE: "receive-form.aspx"
<%
  //  Retrieve input array form values
  string First_Name_str = Form.Request["First_Name[]"];        //  Capture REQUEST variable into string variable
  string[] First_Name_arr = null;                              //  Create a null C# string array variable
  First_Name_arr = First_Name_str.Split(new Char [] {'|'});  //  Split the comma-separated string into the C#.NET string array variable

  //  Loop through form values
  for(i=0; i<First_Name_arr.Length; i++)
  {
    Response.Write(First_Name_arr[i] + "<br>");  //  Output each name on a new line
  }
%>
An added benefit of using an HTML form input array is in JavaScript validation. Traversing through the array of input items is a cinch when HTML input fields have the same name and are of the same datatype. Here's a sample of how you could use JavaScript to validate the HTML form input array fields when the form is submitted:
JavaScript CODE: "input-form.js"
<script type="text\javascript">
//  Copy input array items into JavaScript object variable
var First_Name_obj = document.getElementsByName('First_Name[]');
//  Loop through JS obj and check each item to make sure it is not empty
for (i=0; i<First_Name_obj.length; i++)
{
  //  Check if the value of the item at this array index is empty
  if (First_Name_obj[i].value == "")
  {
    //  Alert the end user that this item has been left empty
    alert("Input item on Row #"+(i+1)+" has been left empty. Please make sure you input something into this field.");
    return false;
  }
}
</script>
The output of the PHP or C#.NET code would look something like the following:
WEB BROWSER VIEW:
John
Jack

Sam
Mike
Mark
Here is the "code behind" page in ColdFusion that I'd use to receive those FORM post values in ColdFusion:
ColdFusion CODE: "receive-form.cfm"
<!---  Retrieve form values --->
<cfset First_Name_list = form.First_Name[]>                      <!---  Capture REQUEST variable into string variable --->
<cfset First_Name_arr = ListToArray(form.First_Name_list, ",")>  <!---  Split the comma-separated string into a PHP array --->

<!---  Loop through form values --->
<cfloop from="1" to="#ArrayLen(First_Name_arr)#" item="i">
  <cfoutput>#First_Name_arr[i]#<br></cfoutput>    <!---  Output each name on a new line --->
</cfloop>
When ColdFusion receives the HTML input array, it immediately converts it into a ColdFusion datatype called a List. ColdFusion Lists are different from ColdFusion Arrays, mainly in that ColdFusion Lists are text strings that use a delimiter, and ColdFusion Arrays have a 1-based index (yes, not 0-based!) that is used to reference the data at that position in the array. ColdFusion Lists cannot by definition contain a null or empty string variable, and so when ColdFusion parses the HTML input form elements, it will drop any items that did not have values given to them before the time of form submission. The output of the ColdFusion code from the same HTML input form used before would look something like the following:
WEB BROWSER VIEW:
John
Jack
Sam
Mike
Mark
Notice the missing "empty" line for our 3rd input item that was left blank? It has been removed by the ColdFusion parser and thus the indices of items 4-6 have been shifted to indices 3-5. As you can imagine, this becomes a serious problem if you are using multiple HTML form input array items (ie: Last_Name, MI, and First_Name) and some of the fields are left blank by the user who is filling out the HTML form.


THE COLDFUSION GURU:

After finding myself quite frustrated with how ColdFusion handles these input arrays, I decided to put together a well-thought question and fire it off to Ben Forta. To my great suprise, he wrote me back the very next morning with the following response:
EMAIL VIEW: Ben Forta writes:
Justin,

Form fields are simple values, not arrays. PHP may do you a favor of turning them into arrays, and CF does the same but turns them into lists. Which is why you can't refer to the field with array syntax, but you could use list syntax.

And yes, you are correct, the lists eliminate unused elements (don't ask why, it's a leftover from a decision a decade ago).

For what it's worth, the "recommended" solution (and this is a general HTML recommendation, not a CF one) is to not reuse field names. In fact, in newer languages, like MXML, that is now illegal. What many CFers do is name the fields FirstName_1 FirstName_2 and so on, that makes it easy to build these programmatically, and also makes it easy to loop through them in the action page.

Of course, you could then create a simple function that turned those into an array, if you are more comfortable with that syntax.

I hope this helps.

--- Ben

THE FIX:

So by now you are wondering how we'll be able to achieve the same functionality in ColdFusion as we currently have in PHP and C#.NET. The answer isn't exciting, and is a bit more difficult programmatically, but it IS possible to achieve the same functionality. You just have to be a bit more careful about how you go about things, so if you are methodical in your process, you'll be ok.

The NEW HTML input form should look like the following:
HTML CODE: "input-form2.html"
<!--  NEW HTML Input Array Form -->

<form id="FirstNameInputForm" name="FirstNameInputForm" method="post" action="receive-form2.cfm">

  <input type="text" name="First_Name_1" size="20" maxlength="30" value="John">
  <input type="text" name="First_Name_2" size="20" maxlength="30" value="Jack">
  <input type="text" name="First_Name_3" size="20" maxlength="30" value="">
  <input type="text" name="First_Name_4" size="20" maxlength="30" value="Sam">
  <input type="text" name="First_Name_5" size="20" maxlength="30" value="Mike">
  <input type="text" name="First_Name_6" size="20" maxlength="30" value="Mark">
  
  <input type="submit" name="submit" value="Submit Form">
  
</form>
The NEW JavaScript validation code for this input page could look like the following:
JavaScript CODE: "input-form2.js"
<script type="text\javascript">
//  Copy HTML form into JavaScript object variable
var myForm_obj = document.getElementById('FirstNameInputForm');
var elementName = "First_Name";

//  Loop through JS obj and check each item by name to make sure it is not empty
for(i=0; i<myForm_obj.length; i++)
{
  //  If this array item has a "name" attribute with the first chars being equal to the elementName variable
  if(myForm_obj[i].name.substring(0,elementName.length) == elementName)
  {
    //  If this item has no value assigned to it
    if (myForm_obj[i].value == "")
    {
      alert("You have left an input item blank for First Name. Please make sure you input something into this field.");
      return false;
    }
  }
}
</script>
The NEW ColdFusion form response page should look like the following:
ColdFusion CODE: "receive-form2.cfm"
<!---  Retrieve form values from FIELDNAMES CF variable and loop through them --->
<cfloop list="#form.FIELDNAMES#" delimiters="," index="i">
  <!---  If the first characters of this HTML form field include "First_Name" --->
  <cfif Left(i, Len("First_Name")) IS "First_Name">
  
    <cfoutput>#form.i#<br></cfoutput>    <!---  Output each name on a new line --->
    
  </cfif>
  
</cfloop>
The output of this new ColdFusion code would look something like the following:
WEB BROWSER VIEW:
John
Jack

Sam
Mike
Mark
There you have it. How to programmatically use HTML form input arrays (well, these are actually not real arrays) with ColdFusion 7. Now try out these samples and get back to coding!

- Justin Tubbs