[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

Trying to set a Variable to a form value...

Okay, I've been tearing my hair out all morning over this.

I'm creating a dynamic set of forms, in which a user progresses through (answers a question, goes to the next, answers that, etc). I created a template which gets included on every one of these pages the user goes through, called answers.cfm.
In answers.cfm, a query gets all the questions in the database, then steps through the questions one at a time to see if FORM.Question# exists, if it does, it outputs the Question #, the Question, and the user's answer to it, and sticks a hidden field with the same name so that it's passed to the next form as well.

If that didn't confuse you, here's my problem. It all works grand up until I try to set the hidden field's value equal to the field that was passed and then print out the contents of that field. I know my problem is that I'm setting the variable equal to the NAME of the field and not the value, but I'm stuck! I've tried about a hundred different ways to do this and I either get errors because of syntax or I simply get the NAME of the field.

Can anyone help?

<cfquery name="getAnswers" datasource="MyData">
SELECT         QuestionID, QuestionText
FROM         Questions
</cfquery>
<table>
<cfoutput query="getAnswers">
<cfif IsDefined ("FORM.Question#getAnswers.QuestionID#")>
<cfset Question = "Question" & "#getAnswers.QuestionID#">
<cfset MyFORM = "FORM.Question" & #getAnswers.QuestionID#>
      <tr>
            <td>
            <div class="answers">#Variables.Question#. #getAnswer.QuestionText#</div>
            </td>
            <td>
<input type="hidden" name="#Variables.Question#" value=#Variables.MyFORM#>
<div class="answers">#Variables.MyFORM#</div>
            </td>
      </tr>
</cfif>
</cfoutput>
</table>

0
Sianne
Asked:
Sianne
  • 5
  • 5
  • 2
  • +3
1 Solution
 
SianneAuthor Commented:
Edited text of question.
0
 
SianneAuthor Commented:
Adjusted points to 50
0
 
SianneAuthor Commented:
Adjusted points to 100
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
dapperryCommented:
I'm not sure if this is all of your problem, but this line of your code won't work:

<cfif IsDefined ("FORM.Question#getAnswers.QuestionID#")>

You need to use the Evaluate function, something like this:

<cfif IsDefined(Evaluate("Form.Question" & getanswers.QuestionID))>

Try that, and let me know how it goes.

:) dapperry
0
 
anushaCommented:

<input type="hidden" name="#Variables.Question#" value=#Variables.MyFORM#>

Will not pass value as the name field is not written correctly.
Name tag should not contain variables as follows #xxxxx#, although you can access the passed variable by #name# int he next form.

Example..in asnwers.cfm include following

<input type="hidden" name="Question" value=#Variables.MyFORM#>

In the next template you can access that variable as follows.. (answer2.cfm)

#form.question#      
<!--- Will print the value passed (#Variables.MyFORM#) --->

Also, I think I know what you are trying to accomplish here, 2 dynamic states at the same time, which is tricky.
Try having a global.cfm with a CFLOOP to run through the questions ID's while checking its existence and then run CFquery within the CFLOOP.
which creates mini-cfquery statements for each iteration of questionID's.

Hope this helps.
Anusha

0
 
SianneAuthor Commented:
dapperry: <cfif IsDefined ("FORM.Question#getAnswers.QuestionID#")>

Not the problem. It actually does work. May be incorrect, but it does work.

anusha:

While it may be that name parameters should not contain #variables#, this does work, and it's not the problem. I've successfully used it elsewhere.

The problem is that with <cfset MyFORM = "FORM.Question" & #getAnswers.QuestionID#>, I am attempting to set a variable to the *value* of the form field name I am concatinating, but in reality, all I am doing is setting the variable to the *name* of the form field.

<input type="hidden" name="#Variables.Question#" value=#Variables.MyFORM#>
What I want value=#Variables.MyForm# to do is take MyForm as a value and not as a name of a field. In otherwords, say this runs once. Variables.MyFORM would be equal to "FORM.Question1". Not the *value* of FORM.Question1, but the name. When I assign the value to the hidden field, I want to assign it the *value* of the *name* of the field I am passing it.

In essence, I want something which will allow me to do:
<input type="hidden" name="#Variables.Question#" value="##Variables.MyFORM##">
Which of course, doesn't work. I've tried parens, pounds, single quotes, double quotes, the works.

I'm attempting to make a teeny bit of code that will handle a lot of the work, so I don't have to hand code the answers being printed out.

There has to be a way this is possible.
0
 
dapperryCommented:
What about:

<cfset MyFORM = "FORM.Question" &
                Evaluate(getAnswers.QuestionID)>

Does that give you what you want?

:) dapperry
0
 
SianneAuthor Commented:
dapperry: No, that still sets the variable equal to the variable name, not the value. You really like that evaluate function, don't you?
0
 
BrigidCommented:
LOL!
See if #SetVariable(#variable#, #value#)# will work for you, Sianne.
0
 
dapperryCommented:
Then how about:

<cfset MyFORM = "FORM.Question" & 
                                DE(getAnswers.QuestionID)>

or

<cfset MyFORM = "FORM.Question" & 
                                DE(Evaluate(getAnswers.QuestionID))>

:) dapperry

BTW - Yeah I use Evaluate and DE a lot.
0
 
dapperryCommented:
Oh yeah, or

<cfset MyFORM = "FORM.Question" & 
             (Evaluate(DE(getAnswers.QuestionID))>

Any of those work?

:) dapperry
0
 
dapperryCommented:
Actually, after reading more carefully (Which is probably what I should have done in the first place!) You probaly need:

<cfset MyFORM = Evaluate("FORM.Question" & 
           getAnswers.QuestionID)>

or something like that.

:) dapperry
0
 
rod_nolanCommented:
I'm not sure what you want to do with these questions and answers at the end of the process but it sounds like you could use Ben Forta's custom tag, CF_EmbedFields.

It's about 12 lines of code and you'll probably find it in the Tag Gallery at Allaire's site, at Ben's site (www.forta.com) or in his book, Advanced ColdFusion Application Development (p. 118). Basically, this tag takes care of passing all the form controls and their values along through all the pages in a multipart form so that you can process them in your final action page as if they were all present in the last page.

I've spent some time playing around with your particular problem and a number of questions arise.

So far, we've all been attempting to solve this problem by looking at the syntax. What about looking at a  different approach? It seems that you are trying to do some pretty complex processing when there may be a simpler way to accomplish your goal. First, let me state my understanding of the problem to make sure that I'm on the right track.


> In answers.cfm, a query gets all
> the questions in the database, then
> steps through the questions one at
> a time to see if FORM.Question#
> exists, ... if it does, it

You're running a query to retrieve all questions and their IDs. You are looping through the result set, attempting to pinpoint the specific question that was just answered on the previous form by comparing the name of the form control with a value that consists of the hardcoded word "Question" concatenated with the questionID of the question that was answered on the previous page.

This line ...

<cfif IsDefined ("FORM.Question#getAnswers.QuestionID#")>

.... means that you expect the form to contain a control called questionX, where _n_ is the question ID of the question that was just answered.

 
So your form will contain a control called QuestionX, where X is the questionID and you're going to go through each and every question in the database and compare it's ID with an already known value.


First, this isn't a very efficient approach and Second, I wonder how you plan to manage that form control naming scheme? Will you hard code it (bad idea), will you generate the form control name with the results of a query (better idea, more on that below)?


On we go...


> it outputs the Question #, the
> Question, and the user's answer to
> it, and sticks a hidden field with
> the same name so that it's passed to
> the next form as well

I'm not entirely sure what you mean by "... a hidden field with the same name so that it's passed to ..." What value are you trying to pass in the hidden form field and what name are you trying to give it?

It would be helpful to see the form page because, from what I can gather, you are trying to test for the existence of a form variable that is being created dynamically. This just seems like a difficult process to manage.

Here's my suggestion. Why not just create a hidden form control on the form page with the value of the question ID? You can use that form variable (form.questionID) to do a query on the next page that will give you the ID and question text.

[... in the form page ...]

I'm assuming that you are retrieving the next question that you want to ask with a query called "getQuestionToAsk"

<cfoutput query="getQuestionToAsk">
  <input type="hidden" name="getQuestionToAsk.QuestionID" value="#QuestionID#"><br>

  #getQuestionToAsk.QuestionText#<br>

  <input type="Text" name="Answer">
</cfoutput>

[... in the action page ...]

<cfquery name="getAnsweredQuestion">
  SELECT questionID, questionText
  FROM Questions
  WHERE questionID = 'form.questionID'
</cfquery>

Your question was:
<cfoutput query="getAnsweredQuestion">
  #getAnsweredQuestion.QuestionID#
  #getAnsweredQuestion.QuestionText#
</cfoutput>
and your answer was
<cfoutput>
#form.answer#
</cfoutput>

This is just a suggestion that may or may not work within the larger scheme but we can't tackle that until we know the details.

I'd suggest posting the form page and the action page so we can get a better understanding of exactly how your code is working. And let us know what you plan to do at the end, when all the questions have been answered. Chances are that the CF_EmbedFields tag is all you need.


Good luck,
Rod
0
 
rod_nolanCommented:
Sorry, I just noticed an error in my last message! Near the end I explained how I'd set up the form and action pages and I reversed the name and value attributes of the QuestionID hidden field. That piece should read as follows:

<cfoutput query="getQuestionToAsk">

<input type="hidden"
 name="QuestionID"
 value="#getQuestionToAsk.QuestionID#">

 #getQuestionToAsk.QuestionText#<br>
<input type="Text" name="Answer">
<input type="submit" value="Next">

</cfoutput>


Also, Sianne, I hope that my statement on the inefficiency of your query code didn't sound too harsh. I didn't mean to criticize, I was just thinking that if you already know the value of the QuestionID, then you could skip the loop and go straight to the record you require by adding the QuestionID to your query in a WHERE clause. Again, seeing your actual templates would help us get a better sense of exactly how your code is working.

Rod
0
 
timwrCommented:
I believe you need an evaluate in this line:
<cfset MyFORM = "FORM.Question" & #getAnswers.QuestionID#>
which sets the variable MyFORM to the name of the Question instead of the answer to the question. Try this if you haven't already:

<cfset MyFORM = evaluate("FORM.Question" & #getAnswers.QuestionID#)

Tim R.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 5
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now