Skip to main content
Inspiring
August 20, 2009
Question

<cfif structkeyexists(form, "btnSubmit")> overridding

  • August 20, 2009
  • 2 replies
  • 2404 views

I have a form that submits to itself instead of an action page.

This is my submit button : <input type="submit" name="btnSubmit" style="width:  108px" value="Submit">

When the form is submitted, I check for the existence of the submit button and then do processing :

<cfif structkeyexists(form, "btnSubmit")>

<cftransaction>

<!--- Deteremine if any of the checkboxes were checked from the form. Since the checkboxes name is 'del=#urdn_number#',
check to see if the first four characters are 'Del_'. If it is, perform a loop and extract the the urdn number into
the variable select_urdn_number --->
<cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">
<cfloop index="i" list="#form.fieldnames#" delimiters=",">
<cfset select_urdn_number = listlast(i,"_")>

I basically have rows of data and each row contains a checkbox and I process all the transactions simultaneously that are checked.

What is happening now is that it seems to be treating form.btnSubmit as a form variable and attempting to insert it into the table and it is blowing up.

When I did this using an action page, there was no need to check for form.btnSubmit and the inserts worked without any problems.

How can I get around this problems ?

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    August 22, 2009

    Two things:

    1) <cfif structkeyexists(form, "btnSubmit")> is the same as <cfif isDefined("form.fieldnames")>

    2) mid(form.fieldnames,1,4) looks at the comma-delimited list of field names, instead of at one particular field name

    If you take these two points into account, then the code

    <cfif structkeyexists(form, "btnSubmit")>
        <cftransaction>
            <cfif isDefined("form.fieldnames") and mid(form.fieldnames,1,4) is "Del_">
                <cfloop index="i" list="#form.fieldnames#" delimiters=",">
                    <cfset select_urdn_number = listlast(i,"_")>
                    <!--- etc., etc. --->
                </cfloop>
            </cfif>
        </cftransaction>
    </cfif>

    will be corrected to

    <cfif structkeyexists(form, "btnSubmit")>
        <cftransaction>
            <cfloop index="i" list="#form.fieldnames#" delimiters=",">
                <cfif mid(i,1,4) is "Del_">
                    <cfset select_urdn_number = listlast(i,"_")>
                    <!--- etc., etc. --->
                </cfif>
            </cfloop>
        </cftransaction>
    </cfif>

    Inspiring
    August 20, 2009

    Post via email trashed by forum software

    Inspiring
    August 20, 2009
    <cfif isDefined("form.fieldnames") and
    mid(form.fieldnames,1,4) is "Del_">

    If you only want to capture fields that start with "Del_", you must check the _names_ of each field as you loop through them.  Not the form.fieldnames value.

    <cfloop index="currentField" list="#form.fieldnames#">
       <cfif mid(currentField,1,4) is "Del_">
            ... do something with matching field
       </cfif>
    </cfloop>

    But if all you need is a list, why name the checkboxes individually at all? Just give the checkboxes the same name and make the "urdn" the checkbox value.

    <input name="del" value="#urdn#">
    <input name="del" value="#urdn#">
    <input name="del" value="#urdn#">
    ...

    Then the selected values will be submitted as a clean comma delimited list.  No need to go through the hassle of parsing form field names.

    trojnfnAuthor
    Inspiring
    August 20, 2009

    Moving the cfif inside the loop instead of outside seems to do the job, no more trying to submit form.btnSubmit as a form variable.

    Now on a related note, for every record that was checked, I want to output those into a report, to show which ones were checked. So if four were checked, I want a report with those four records. But with the loop, it is producing four separte reports, one for each check.

    I thnk I still need the loop, but how can I just produce one report with four records instead of four reports ?