Hopefully simple syntax issue with forms

Ask a Question related to Macromedia ColdFusion, Design and Development.

  1. #1

    Default Hopefully simple syntax issue with forms

    First let me say that I am relatively new to ColdFusion. Hopefully you gurus
    out there will be able to steer me int he right direction.

    I have a form that has radio buttons listed for a number of questions. Each
    set of radio buttons is named on the form based on a question id (i.e.,
    <cfinput type="radio" name="questionid#SurveyQuestions.QuestionID#" value="a
    value">). When I go to the form handler and try to set a variable equal to
    that value, I'm having a problem. I am also sending a list of the question ids
    as a hidden field to the form handler.

    On the form handler, I thought I could just do something like:

    <cfloop index="FormQuestionID" list="#Form.QuestionIDs#">
    <cfset Responses = Responses & ", " & #FormQuestionID#>
    </cfloop>

    The #FormQuestionID# comes across okay when I just cfoutput it (i.e.,
    "Form.questionid5", but when I use it here it's giving me the a list of values
    like "Form.questionid5, Form.questionid15, Form.questionid16,
    Form.questionid17" for Reponses. So it's giving me the string value of
    "Form.questionid5" instead of the form value of questionid5, which should be a
    numeric value.

    Any ideas how I could fix this or code this better?

    Thanks!

    Bill

    bill_doe Guest

  2. Similar Questions and Discussions

    1. SQL syntax issue
      Hi all, I'm using CFMX (v6), by the way. I'm try to cobble together a search page that will take the information input on a form and search...
    2. Simple syntax question
      Hi I don't remember how to get the diplayed result broken in multiple lines. The command: mysql> SHOW INDEX FROM recentchanges; gives one...
    3. dummy here needs simple MySQL syntax help
      Need to retrieve a specific row from table but only have the record number ( from a previous queryname.currentrow) passed as a form var to reference...
    4. simple syntax issue?
      Hi, I just want a function to return true, but after a LoadVars.onLoad event. Am I right in assuuming that the return in the onLoad = function...
    5. Simple PHP Headers Syntax Question
      What is the correct syntax to add a variable to a query string using the GET method of a form? I am trying to pass a dynamic variable ('$username')....
  3. #2

    Default Re: Hopefully simple syntax issue with forms

    Form fields are passed to ColdFusion as a structure. If you know the name of
    the field, you can refer to it in dot notation in a format such as:
    Form.questionID25

    The other way to refer to it is in associate array syntax: Form["questionID25"]

    This syntax is also useful in a case such as yours, where the field name is a
    variable. Try something like this:

    <cfloop index="FormQuestionID" list="#Form.QuestionIDs#">
    <cfset Responses = Responses & ", " & Form["#FormQuestionID#"]>
    </cfloop>

    -Paul


    dempster Guest

  4. #3

    Default Re: Hopefully simple syntax issue with forms

    Thanks Paul. I tried it, but now I'm getting the error below. FYI,
    "questionid5" is the first value in the #Form.QuestionIDs# list.

    Thanks!

    Bill

    "Element questionid5 is undefined in a Java object of type class
    coldfusion.filter.FormScope referenced as

    The error occurred in
    C:\Inetpub\wwwroot\Win2K_Desktop\Survey\survey_tha nks.cfm: line 35

    33 : <cfset Responses = "">
    34 : <cfloop index="FormQuestionID" list="#Form.QuestionIDs#">
    35 : <cfset Responses = Responses & ", " & Form["#FormQuestionID#"]>
    36 : </cfloop>
    37 : <cfoutput>#Responses#</cfoutput><p>"

    bill_doe Guest

  5. #4

    Default Re: Hopefully simple syntax issue with forms

    If the form element numbers are consecutive (ie. with no gaps), you could just
    store the total number of questions in your hidden form field, and on the
    action page, loop from 1 to the #form.totalNumberOfQuestions# and retrieve the
    answer for each question. Note: Since, you're using radio buttons, you may
    need to use CFPARAM to ensure the form fields exist on the action page.



    <!--- example: if you had 15 questions --->
    <cfset form.totalNumberOfQuestions = "15">

    <cfset Responses = "">
    <cfloop from="1" to="#form.totalNumberOfQuestions#" index="counter">
    <cfset Responses = ListAppend(Responses, Form["question"& counter])>
    </cfloop>

    <cfoutput>
    #Responses#
    </cfoutput>

    mxstu Guest

  6. #5

    Default Re: Hopefully simple syntax issue with forms

    Are you sure that that the form field questionid5 is being sent?

    You may need to check that a particular form field was submitted. Text fields
    are always submitted even if they are blank, but radio buttons and checkboxes
    would not be submitted if a selection was not made. In those cases, you'd have
    to check that a field exists first. In dot notation you could use
    IsDefined("Form.questionid5"). Checking as a structure, you'd use
    StructKeyExists(structure, key) to see if it is defined.

    -Paul


    dempster Guest

  7. #6

    Default Re: Hopefully simple syntax issue with forms

    Yeah, that was the first thing I checked. I just did a <cfoutput>#Form.response5#</cfoutput> and the value is definitely there.
    bill_doe Guest

  8. #7

    Default Re: Hopefully simple syntax issue with forms

    If the field is named "response5" then shouldn't the list of form field names be "response5,etc..." and not "questionid5, etc"...?
    mxstu Guest

  9. #8

    Default Re: Hopefully simple syntax issue with forms

    Hey mxstu, I tried this code, but it looks like it's putting a space between
    "question" and the value of counter.

    "Element question 5 is undefined in a Java object of type class
    coldfusion.filter.FormScope referenced as

    The error occurred in
    C:\Inetpub\wwwroot\Win2K_Desktop\Survey\survey_tha nks.cfm: line 91

    89 : <cfset Responses = "">
    90 : <cfloop list="#Form.QuestionIDs#" index="counter">
    91 : <cfset Responses = ListAppend(Responses, Form["question" & counter])>
    92 : </cfloop>
    93 : "

    bill_doe Guest

  10. #9

    Default Re: Hopefully simple syntax issue with forms

    Sorry, I mistyped. I corrected in almost right away, but you must have seen my mistake before I corrected it!
    bill_doe Guest

  11. #10

    Default Re: Hopefully simple syntax issue with forms

    Two things, I realized your form fields have "ID" in the name, so you need to
    change this statement
    <cfset Responses = ListAppend(Responses, Form["question"& counter])>
    to
    <cfset Responses = ListAppend(Responses, Form["questionID"& counter])>

    Hey mxstu, I tried this code, but it looks like it's putting a space between
    "question" and the value of counter.

    No, this syntax should not add a space. If the value of counter is 5, then
    the output would be question5 (no space)


    mxstu Guest

  12. #11

    Default Re: Hopefully simple syntax issue with forms

    Oh, I see that you're using different code than what I posted in my example.

    If the there is a space in the output question 5 then your list must contains spaces.
    mxstu Guest

  13. #12

    Default Re: Hopefully simple syntax issue with forms

    Roger that. Got it working. Thank you guys so very much for your help!
    bill_doe Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139