Link to home
Start Free TrialLog in
Avatar of tags266
tags266

asked on

CFPARAM for form submittal

I have an output that lists products from my db.  The quantity is a text input with a default value of 0.  THe user can batch add numbers and then press submit to add all items to cart on the fly.  If a user deletes the 0 value in the text input and then tries to submit i get an error as if that part of the form is not an element at all.  Please help:

Heres my output
------------------------------------
<cfloop query="getproducts">
<tr>
                  <td width="108" class="txt_gray_11"><!---<a href="/products/productDetails.cfm?product_id=#getproducts.itemLink#" target="_blank" class="txt_gray_11">--->#itemNumber#<!---</a>---></td>
                  <td width="144" class="txt_gray_11">
                         <A HREF="javascript:popUp('invproductViews.cfm?invproduct_id=#invproduct_id#')" class="txt_gray_11" a style="text-decoration:underline">#description#</A></td>
                  <td class="txt_gray_11">#color_name#</td>
                  <td class="txt_gray_11">#size_name#</td>
                  <td width="53" class="txt_gray_11_strike">#dollarformat(msrp)#</td>
                  <td width="55" class="txt_gray_11">#dollarformat(price)#</td>
                  <td width="94" class="txt_gray_11"><input type="text" name="quantity" value="0" class="txt_gray_11" size="2">
            <input type="hidden" name="invproduct_id" value="#invproduct_id#">
            <input type="hidden" name="itemnumber" value="#itemnumber#">
            <input type="hidden" name="description" value="#description#">
            <input type="hidden" name="price" value="#price#"></td>
                </tr>

</cfloop>


HERES MY PROCESSING PAGE
-----------------------------------------------------
<cfif isdefined('form.add_button.y')>

<cfloop from="1" to="#listlen(form.invproduct_id)#" index="i">

<cfset invID = listGetAt(form.invproduct_id, i)>
<cfset iNumber = listGetAt(form.itemnumber, i)>
<cfset Descr = listGetAt(form.description, i)>
<cfset pr = listGetAt(form.price, i)>
 
   <cfset quan = listGetAt(form.quantity, i)>
 
<cfif quan NEQ 0>

      <cfset bMatching = false>
      <cfloop from="1" to="#arraylen(session.merchcart)#" index="i">
            <cfif session.merchcart[i].invID eq invID>
                <cfset bMatching = true>
                  <cfset session.merchcart[i].quan = session.merchcart[i].quan + quan>
                  
        </cfif>
      </cfloop>
</cfif> etc....


HERES MY ERROR
---------------------------------
Invalid list index 159.  
In function ListGetAt(list, index [, delimiters]), the value of index, 159, is not a valid as the first argument (this list has 158 elements). Valid indexes are in the range 1 through the number of elements in the list.  
 
The error occurred in : line 18
 
16 : <cfset pr = listGetAt(form.price, i)>
17 :  
18 :    <cfset quan = listGetAt(form.quantity, i)>
19 :  
20 : <cfif quan NEQ 0>

 
Avatar of rucky544
rucky544
Flag of United Kingdom of Great Britain and Northern Ireland image

i would use isdefined to check if it is submitted with a value.
I would also check that it isnumeric as this will probably cause errors to.

use something like.

<cfif isdefined("form.quantity") and isnumeric(form.quantity) is "yes">
   <cfset form.quantity = 0>
</cfif>

This will use the value 0 if it is not defined or not a number. If this is put at the top of the code then it will use that zero instead of what was submitted. If a number was submitted then it will not set form.quantity to 0.
Avatar of tags266
tags266

ASKER

I'm not sure i follow, can you use it in my code?  Does it fit within the cfloop ??
just place it above all your code. In the <head> would be fine.

It checks that form.quantity exists and that it is a number, if it's not then it sets form.quantity to 0.

So from then on it will be used as if the user had submitted a 0 in the form. Which i am guessing would not put anything in to your basket, you could always set it to 1 if you want just one item added.
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tags266

ASKER

gdemaria,

Thanks for a very thorough and detailed analysis.  I see exactly what you are saying and it explains perfectly the problem I am running into.  However, im having a hard time fitting it into my code.  Where at exactly? Am i using it in the list loop or above it?  I feel i'm almost there.

you should use it on your form processing page, above any other code that is going to be using form.quantity.

you could even put it above all the html on the page before <html> tag. In line 1.

It doesn't really matter where it is, as long as it is above the first time form.quantity is used.

Glad i have been able to sort this one out for you.
Avatar of tags266

ASKER

Gdemaria,

I GOT IT!.... I needed to change <cfloop from="1" to="#listlen(form.invproduct_id)#" index="i"> to
<cfloop from="1" to="#listlen(form.quantity)#" index="i">

rucky, sorry your solution did not work within my list code.  I do appreciate your effort and attempts however, Thanks.

>  GOT IT!.... I needed to change <cfloop from="1" to="#listlen(form.invproduct_id)#" index="i"> to  <cfloop from="1" to="#listlen(form.quantity)#" index="i">

I'm not sure just this will do it for you.  What you're doing is not looping for every product, but instead looping for every quantity entered.   If one quantity is left blank, you skip that quantity so your loop doesn't go around 5 times it only goes around 4 times.

Also, you could be in trouble if any of these have a blank in them.  The risk is that you may pull the wrong itemNumber or description unintentionally.   Another problem could arrive if your description has a comma in it.  That would act as a delimiter and your description would get broken up.

<cfset invID = listGetAt(form.invproduct_id, i)>
<cfset iNumber = listGetAt(form.itemnumber, i)>
<cfset Descr = listGetAt(form.description, i)>
<cfset pr = listGetAt(form.price, i)>

I recommend switching to the "array style" processing where you set a different name for each field by appending a 1,2,3...

If you choose to, I am happy to assist with this.


Avatar of tags266

ASKER

GRRR!!! you are right, upon further testing i see that it shifts everything up after the null value is placed.

I was quick to give you your points but would like to switch to the array style as you recommend.  Please assist me with this, just tell me what i need to do and how i can get it to work with the rest of the hidden inputs so that they all get added to my cart.  

Thanks.

 Not a problem, if it was just about points, I wouldn't have pointed out the problem :)


I've abridge the code a bit to show just the pertinent parts.
In short, you append the record number to the end of each field name, then save the total number of records.  When you process it, you loop through the number of records from 1 to X (just like you were looping through your list) and grab the value from the form variable (quantity1, quantity2, quantity3, etc..)   Here it is...

<cfloop query="getproducts">
   <cfset variables.ct = getProducts.currentRow>
 <tr>
   <td width="94" class="txt_gray_11">
   <input type="text"   name="quantity#variables.ct#" value="0" class="txt_gray_11" size="2">
   <input type="hidden" name="invproduct_id#variables.ct#" value="#invproduct_id#">
   <input type="hidden" name="itemnumber#variables.ct#" value="#itemnumber#">
   <input type="hidden" name="description#variables.ct#" value="#description#">
   <input type="hidden" name="price#variables.ct#" value="#price#">
   </td>
</tr>
</cfloop>
<input type="hidden" name="totalRecords" value="#variables.ct#">


HERES MY PROCESSING PAGE
-----------------------------------------------------
<cfif isdefined('form.add_button.y')>

<cfloop index="ii" from="1" to="#form.totalRecords#">

  <cfset invID   = form['invproduct_id' & i]>
  <cfset iNumber = form['itemnumber' & i]>
  <cfset Descr   = form['description' & i]>
  <cfset pr      = form['price' & i]>
  <cfset quan    = form['quantity' & i]>

 oops, i changed your loop variable "i" to "ii"  (i prefer double letters as they're easier to find when searching), but I didn't change them all..

<cfloop index="ii" from="1" to="#form.totalRecords#">

  <cfset invID   = form['invproduct_id' & ii ]>   <--- changed to ii

Avatar of tags266

ASKER

That did it!  Thanks again for your help.  you are an asset to this community.

 Excellant, glad it worked for you!