views:

64

answers:

2

Basically, I have a form that generates a certain number of "types of publications" depending on "departments" input before someone is filling out this form. (Department specific publication types that are recognized.) There are a couple of fields that go with each publication type...(they are the same fields, so each type will have ...let say 3 fields..) I have a loop that reads this data and puts it into the database. However, if one of the fields are not filled out the value for that particular field is completely skipped thus throwing off the input of data.

Example: User has three fields and these three fields are repeated three times. The user fills out three in the first row two in the second row and three again in the third row. So:

  • First field array: 1, 1, 1
  • Second field array: 1, 1
  • Third field array: 1, 1, 1

I need to figure out a way to mark that empty field in the second field array so it will appear in the list. I could set default values to the fields, but someone could easily delete that data and in name/ title fields it would seem tacky to have "None" or something...

Any Ideas?

Edit: Code Snippet (Note: I cut out all the unimportant style type things...)

<cfoutput query = "getType_PUB">
Publications: #rName# <br />
<input type = "hidden" name = "scholarActivities" value = "#rName#" />
<input type="text" name="inpress09"  size = "8"/><br />
<input type="text" name="published09" size = "8"/><br /> 
<input type="text" name="published08" size = "8"/><br />
<input type="text" name="published07" size = "8"/><br />
</cfoutput>

<cfoutput><input type = "hidden" name = "recordcountPub" value = "#getType_PUB.recordcount#" /></cfoutput>


//////////////DB/////////////

<cfif #form.recordcountPUB# EQ 1>
<cfquery name = "insertSActivities" datasource="cas_evaluation">
  INSERT INTO scholar_publications (faculty, scholarActivities, submit09, inpress09, published09, published08, published07)
  VALUES ( '#form.name#', '#form.scholarActivities#', '#form.submit09#', '#form.inpress09#', '#form.published09#', '#form.published08#', '#form.published07#')
  </cfquery>
<cfelse>
<cfloop from="1" to="#form.recordcountPUB#" index="i">
  <cfquery name = "insertSActivities" datasource="cas_evaluation">
  INSERT INTO scholar_publications (faculty, scholarActivities, submit09, inpress09, published09, published08, published07)
  VALUES ( '#form.name#', '#ListGetAt(form.scholarActivities, i, ',')#', '#ListGetAt(form.submit09, i, ',')#', '#ListGetAt(form.inpress09, i, ',')#', '#ListGetAt(form.published09, i, ',')#', '#ListGetAt(form.published08, i, ',')#', '#ListGetAt(form.published07, i, ',')#')
  </cfquery>
</cfloop>
</cfif>
+1  A: 

What field types are these? Checkboxes? If so you can set up default values on the action page, with value of 0 or some other value which indicates a non answer. Then check for that non answer value and skip.

Or is the value passed as blank, and your loop code simply not adding it to the list/array?

A quick code example from you make help people give you a better direction.

Joshua Cyr
"the value passed as blank, and your loop code simply not adding it to the list/array?"
Bri
That is the case^
Bri
Updated the OP with code snippet :)
Bri
No comments on the lack of cfqueryparam... always add those last before going live...I'm just a weirdo :\
Bri
I am guessing you want 0 to be passed in if they didn't check off the box. In that case make a <cfparam> for each form field with default value of 0. It will be overridden by the form for anything checked off, and it will be 0 for anything else. <cfparam name="#form.whatever" default = "0">
Joshua Cyr
Thanks <3 I don't know why I didn't think about it...I forgot they work for non-checkbox input too... DOH
Bri
@Bri - *they work for non-checkbox input too* - Are you talking about CFPARAM? Because it does not work for text boxes and a few other field types that are normally always defined ...
Leigh
Meh... I thought it would work..but it didn't...like leigh said... doesn't work for text boxes...hmmm... back to square one...
Bri
Since they are input boxes, you can then loop over them on the action, replace values of "" with "0" or something along those lines. Actually loop over all form fields and do that. Which is a total hack frankly. You can also make the text input into a drop down (if the values are allowed like that) or use javascript that on submit certain fields change from empty to zero, or add masks on the fields so people must put in 0 and not blank. or use replace() on ',,' to ',0,' or something like that for your lists.
Joshua Cyr
Probably what is best is to rebuild your form so you are passing over data in a better way though.
Joshua Cyr
+2  A: 

(To elaborate on my comments..) It is safer to create unique field names. Otherwise, your INSERT code may break if the user enters a comma in one of the form fields. Since you are already using a query loop you can append #currentRow# to each set of fields to make the names unique

<cfoutput query = "getType_PUB">
    Publications: #rName# <br />
   <input name="scholarActivities#CurrentRow#" ... />
   <input name="inpress09#CurrentRow#" ... />
   <input name="published09#CurrentRow#" ... />
</cfoutput>

<cfoutput>
   <input name="recordcountPub" value="#getType_PUB.recordcount#" .. />
</cfoutput>

On your action page, simply loop and extract the values with array notation. No need for the extra CFIF. You can still use CFPARAM or structKeyExists to handle any fields that may not exist (ie checkboxes or radio buttons).

<cfloop from="1" to="#form.recordcountPUB#" index="i">
   <!--- extract the values ...--->
   <cfset scholarActivities = FORM["scholarActivities"& i]>
   <cfset submit09 = FORM["submit09"& i]>
   ... 
  <cfquery ....>
        Do the INSERT 
  </cfquery>
</cfloop>
Leigh
Thank you :) This is what I was looking for. I originally had these as rows of fields the user could dynamically add in and each row had an appended number 1,2 3...etc. However, I was just looking around at stuff and I felt like having multiple fields was kind of inefficient...This is probably the most advance I have gotten with Coldfusion. I really REALLY appreciate your help! You have no idea! :)
Bri
You are welcome :)
Leigh