Skip to main content
September 19, 2011
Answered

Adding costs in cfloop

  • September 19, 2011
  • 1 reply
  • 778 views

i am trying to  create a simple invoicing script for a friend. I can get the loop to add another row and save the data. My propblem is when I try to add up the material costs. I keep getting the followinf error: Element cost1 is undefined in a Java object of type class coldfusion.filter.FormScope. (line 121)

<!--- Gets the value of getnumba from above --->

        <input type="hidden" name="numba" value="#getnumba#">

        <!--- Loop Through from 1 to the number above --->

       

        <cfloop from="1" to="#getnumba#" index="idx">

            <!--- At first, it displays the original one row form --->

            <!--- User enters the data and hits "Add Another" --->

            <!--- The page reloads with the data already entered and creates a new blank row that is ready for data entry --->

            <tr>

                <cfif isdefined ("form.partno#idx#")>

                    <Td><input type="text" name="partno#idx#" value="#evaluate("form.partno#idx#")#" size="62"></TD>

                <cfelse>

                    <Td><cfinput type="text" name="partno#idx#" size="62"></TD>

                </cfif>

                <cfif isdefined ("form.cost#idx#")>

                    <Td><cfinput type="text" name="cost#idx#" value="#evaluate("form.cost#idx#")#" size="6"></TD>

                <cfelse>

                    <Td><cfinput type="text" name="cost#idx#" size="6"></TD>

                </cfif>

            </tr>

        </cfloop>

<tr>

<td align="right">Materials Total </td>

<td>

   <cfset subcost = 0>

     <cfloop from="1" to="#getnumba#" index="idx">

     <cfset subcost = variables.subcost + form["cost" & idx]> (line 121)

     </cfloop>

     #subcost#

</td>

</tr>

What am I doing wrong?

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    <!--- Gets the value of getnumba from above --->

            <input type="hidden" name="numba" value="#getnumba#">

            <!--- Loop Through from 1 to the number above --->

           

            <cfloop from="1" to="#getnumba#" index="idx">

                <!--- At first, it displays the original one row form --->

                <!--- User enters the data and hits "Add Another" --->

                <!--- The page reloads with the data already entered and creates a new blank row that is ready for data entry --->

                <tr>

                    <cfif isdefined ("form.partno#idx#")>

                        <Td><input type="text" name="partno#idx#" value="#evaluate("form.partno#idx#")#" size="62"></TD>

                    <cfelse>

                        <Td><cfinput type="text" name="partno#idx#" size="62"></TD>

                    </cfif>

                    <cfif isdefined ("form.cost#idx#")>

                        <Td><cfinput type="text" name="cost#idx#" value="#evaluate("form.cost#idx#")#" size="6"></TD>

                    <cfelse>

                        <Td><cfinput type="text" name="cost#idx#" size="6"></TD>

                    </cfif>

                </tr>

            </cfloop>

    <tr>

    <td align="right">Materials Total </td>

    <td>

       <cfif isdefined ("form.cost#idx#")>

       <cfset subcost = 0>

         <cfloop from="1" to="#getnumba#" index="idx">

         <cfset subcost = variables.subcost + form["cost" & idx]>

         </cfloop>


    You need to check each individual field inside the loop, not outside.  The same as you are doing when creating the input fields.

    (beware. these forums eat html tags...)

       cfloop ....

        cfif isdefined ("form.cost#idx#")

           cfset subcost = variables.subcost + form["cost" & idx]

        /cfif

       /cfloop

    1 reply

    Inspiring
    September 19, 2011

    You forgot to verify the form fields exist as you are doing in your first loop.

    September 19, 2011

    Didn't do it. After adding  <cfif isdefined ("form.cost#idx#")> the error does not show, however, neither does the costs once they are entered. I added values and removed the cfif line, uploaded the script and hit refresh and now I have the following:

      

    <cfset subcost = 0>

         <cfloop from="1" to="#getnumba#" index="idx">

         <cfset subcost = variables.subcost + form["cost" & idx]> (line 121)

         </cfloop>

    I am still getting the following error message:

    Element cost2 is undefined in a Java object of type class coldfusion.filter.FormScope. The error occurred in wwwroot\invoice_form.cfm Line 121

    Inspiring
    September 19, 2011

    Added it where? That looks like the same code you had before.  Post your new code.