Solved

I am having a problem with varable undefined error

Posted on 2009-07-08
19
252 Views
Last Modified: 2013-12-20
Hi Folks,

I am having a problem running this script.  The idea is to take specific records and line up each record and its fields for the user to view on the next page.

My problem is I am getting a Variable form.htitle_ is undefined error and its pointing to this line. <cfset hID = Evaluate(hID)>

I hope I have explained this ok.  Please let me know if you need more info.

Thanks!!!

<cfif form.build IS Build KC Report>
 

<cfquery name=deleterecords datasource=#DS#>

Delete from #kcr#

</cfquery>
 

<cfquery name=numrecords datasource=#DS#>

Select count(*) as numRec

From #kch#

Where approver_type = #URL.aid#

</cfquery>
 

<cfoutput query=numrecords>

<cfset recNum = #numRec#>

</cfoutput>

<cfset hCount = 0>
 

<cfloop index=Add from=1 to=#recNum# step=1>

	<cfset hCount= hCount + 1>

	<cfset hID = form.htitle_#hCount#>

	<cfset hID = Evaluate(hID)>

	<cfset rOffice = form.roffice_#hCount#>

	<cfset rOffice = Evaluate(roffice)>

	<cfset rank = form.rank_#hCount#>

	<cfset rank = Evaluate(rank)>
 

<cfquery name=insert_highlights datasource=#DS#>

	Insert into #kcr# (highlight, office, rank)

	Values (#hID#, #roffice#, #rank#)

</cfquery>
 

<cflocation url=view_current_report.cfm?oid=#URL.oid#&aid=#url.aid# addtoken=no>

Open in new window

0
Comment
Question by:Shwapy
  • 8
  • 5
  • 4
  • +1
19 Comments
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
you have 2 varibales with the same name
<cfset hID = form.htitle_#hCount#>
        <cfset hID = Evaluate(hID)>
also why don;t you use index
<cfset hID = form.htitle_#Add#>
0
 
LVL 8

Expert Comment

by:MartinCMS
Comment Utility
look like you're missing the # around the variable on each of the evaluation setting.

<cfset hID = Evaluate(#hID#)>
<cfset rOffice = Evaluate(#roffice#)>
<cfset rank = Evaluate(#rank#)>

0
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
good catch it should be
<cfset hID = #Evaluate(hID)#>
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
We need to see the form code, but the error is pretty clear. The field name you are trying to evaluate does not exist.  There are many causes:
1) using the wrong field name
2)  using <form method="get"..> instead of <form method="post"...>
3) trying to evaluate the fields _before they exist
etc..

>   <cfset hID = form.htitle_#hCount#>
>   <cfset hID = Evaluate(hID)>

     You really do not need to use evaluate().  FORM is a structure, so you can just use array notation
     to access the field names:

    <cfloop from="1" to="10" index="counter">
          <cfset currentRank = FORM["rank_"& counter]>
   </cfloop>

There are a few other problems with the code, but we need to see the <form ..> code first.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
> good catch it should be
> <cfset hID = #Evaluate(hID)#>

No. If they absolutely insist on using evaluate the # signs are not needed:

      <cfset hID = Evaluate(hID)>
0
 
LVL 8

Expert Comment

by:MartinCMS
Comment Utility
Thanks erikTsomik for the correction!
0
 

Author Comment

by:Shwapy
Comment Utility
Thanks guys I will post the form code...
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
Shwapy,

When you get this problem fixed there are also a number of other problems (and potential problems) in the code you may want to fix:

1. These queries are a _big_ sql injection risk. Always use cfqueryparam for parameters like #URL.aid#. Since you cannot use cfqueryparam for table names, if the #kch# and #kcr# tables are user supplied values  (ie #URL.kch# or #FORM.kch# ) they should be validated first.

<cfquery name=numrecords datasource=#DS#>
Select count(*) as numRec
From #kch#
Where approver_type = #URL.aid#
</cfquery>

<cfquery name=deleterecords datasource=#DS#>
Delete from #kcr#
</cfquery>

2. Do not use a query to determine the record count.  HTML is stateless so the data may change in between the query and the loop.  Store the values in a form field instead.

<cfoutput query=numrecords>
<cfset recNum = #numRec#>
</cfoutput>

3. Use cftransaction around the cfloop so you are not left with incomplete data if an error occurs.

4.  Start using quotes "" around values. It is proper coding and will prevent accidental syntax errors

      Use:
      <cfquery name="deleterecords" datasource="#DS#">

      Not:
      <cfquery name=deleterecords datasource=#DS#>

0
 

Author Comment

by:Shwapy
Comment Utility
ok, here is the form that calls the above code...

Thanks


<cfquery name =combine datasource=#DS#>

Select kc_highlights_id, title, office, poc_1, approver_type

From #kch#

Where status=AA4

Order by office

</cfquery>
 

<cfquery name =numRecords datasource=#DS#>

Select count(*) as numRec

From #kch#

Where status=AA4

</cfquery>
 

<cfquery name =get_office datasource=#DS#>

Select * from #kco#

</cfquery>
 

<cfset office_names= ValueList(get_office.office_name)>

<cfset rank_num= 1,2,3,4,5,6,7,8,9,10>
 

<html>

<head>

<cfform action=build_kc.cfm?oid=#URL.oid#&aid=#url.aid#>
 

<table&&>
 

<cfset rCount =0 >

<cfoutput query=combine>

<cfset kid= #kc_highlights_id#>

<cfset rCount=rCount + 1>

<cfif CurrentRow MOD 2 IS 1>

	<cfset class = Blue_TD>

<cfelse>

	<cfset class = White_TD>

</cfif>

<tr>

<td class=#class#><a href=update_kc.cfm?id=#URLEncodedFormat(Trim(kid))#& oid=#URL.oid#&aid=#url.aid#=URL.aid#&at=#approver_type#>#title#</a>

<input type=hidden name=htitle_#rCount# value=#kid# ></td><td class=#class#><select name=rOffice_#rCount# class=KC_office>

	<option value=XO>XO</Option>

				

				

	</option>

</select></td>
 

<td class=#class#><select name=rank_#rCount# class=KC_office>

	<option value=1>1</Option>

				

				

	</option>

	</select></td>
 

</tr>

</cfoutput>

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
by look at the I can say that how you do not have write the code, there is double and single qoates anywhere. And will not sure that the query will return something anyway.

Even here
cfquery name =combine datasource=#DS#>
Select kc_highlights_id, title, office, poc_1, approver_type
From #kch#
Where status=AA4
Order by office
</cfquery>

Should be
cfquery name ="combine" datasource=#DS#>
Select kc_highlights_id, title, office, poc_1, approver_type
From #kch#
Where status=<cfqueryparam cfsql_type="varchar" value="AA4">
Order by office
</cfquery>


and so on and so forth
0
 

Author Comment

by:Shwapy
Comment Utility
Sorry for some reason when I pasted the code in "Attach Code Snippet" it took out the quotes.......


<cfquery name =combine datasource=#DS#>
Select kc_highlights_id, title, office, poc_1, approver_type
From #kch#
Where status=AA4
Order by office
</cfquery>
 
<cfquery name =numRecords datasource=#DS#>
Select count(*) as numRec 
From #kch# 
Where status=AA4
</cfquery>
 
<cfquery name =get_office datasource=#DS#>
Select * from #kco#
</cfquery>
 
<cfset office_names= ValueList(get_office.office_name)>
<cfset rank_num= 1,2,3,4,5,6,7,8,9,10>
 
<html>
<head>
<cfform action=build_kc.cfm?oid=#URL.oid#&aid=#url.aid#>
 
<table&&>
 
<cfset rCount =0 >
<cfoutput query=combine>
<cfset kid= #kc_highlights_id#>
<cfset rCount=rCount + 1>
<cfif CurrentRow MOD 2 IS 1>
     <cfset class = Blue_TD>
<cfelse>
     <cfset class = White_TD>
</cfif>
<tr>
<td class=#class#><a href=update_kc.cfm?id=#URLEncodedFormat(Trim(kid))#& oid=#URL.oid#&aid=#url.aid#=URL.aid#&at=#approver_type#>#title#</a>
<input type=hidden name=htitle_#rCount# value=#kid# ></td><td class=#class#><select name=rOffice_#rCount# class=KC_office>
     <option value=XO>XO</Option>
                 
                 
     </option>
</select></td>
 
<td class=#class#><select name=rank_#rCount# class=KC_office>
     <option value=1>1</Option>
                 
                 
     </option>
     </select></td>
 
</tr>
</cfoutput>
0
 

Author Comment

by:Shwapy
Comment Utility
<cfquery name =combine datasource=#DS#>
Select kc_highlights_id, title, office, poc_1, approver_type
From #kch#
Where status=AA4
Order by office
</cfquery>

<cfquery name =numRecords datasource=#DS#>
Select count(*) as numRec
From #kch#
Where status=AA4
</cfquery>

<cfquery name =get_office datasource=#DS#>
Select * from #kco#
</cfquery>

<cfset office_names= ValueList(get_office.office_name)>
<cfset rank_num= 1,2,3,4,5,6,7,8,9,10>

<html>
<head>
<cfform action=build_kc.cfm?oid=#URL.oid#&aid=#url.aid#>

<table&&>

<cfset rCount =0 >
<cfoutput query=combine>
<cfset kid= #kc_highlights_id#>
<cfset rCount=rCount + 1>
<cfif CurrentRow MOD 2 IS 1>
      <cfset class = Blue_TD>
<cfelse>
      <cfset class = White_TD>
</cfif>
<tr>
<td class=#class#><a href=update_kc.cfm?id=#URLEncodedFormat(Trim(kid))#& oid=#URL.oid#&aid=#url.aid#=URL.aid#&at=#approver_type#>#title#</a>
<input type=hidden name=htitle_#rCount# value=#kid# ></td><td class=#class#><select name=rOffice_#rCount# class=KC_office>
      <option value=XO>XO</Option>
                        
                        
      </option>
</select></td>

<td class=#class#><select name=rank_#rCount# class=KC_office>
      <option value=1>1</Option>
                        
                        
      </option>
      </select></td>

</tr>
</cfoutput>
0
 

Author Comment

by:Shwapy
Comment Utility

sorry guys,

Lets try this again



<cfquery name ="combine" datasource=#DS#">
Select kc_highlights_id, title, office, poc_1, approver_type
From #kch#
Where status='AA4'
Order by office
</cfquery>

<cfquery name ="numRecords" datasource="#DS#">
Select count(*) as "numRec"
From "#kch#"
Where status='AA4'
</cfquery>

<cfquery name ="get_office" datasource="#DS#">
Select * from #kco#
</cfquery>

<cfset office_names= ValueList(get_office.office_name)>
<cfset rank_num= "1,2,3,4,5,6,7,8,9,10">

<html>
<head>
<cfform action="build_kc.cfm?oid=#URL.oid#&aid=#url.aid#">

<table&&>

<cfset rCount =0 >
<cfoutput query="combine">
<cfset kid= #kc_highlights_id#>
<cfset rCount=rCount + 1>
<cfif CurrentRow MOD 2 IS 1>
      <cfset class = "Blue_TD">
<cfelse>
      <cfset class = "White_TD">
</cfif>
<tr>
<td class="#class#"><a href="update_kc.cfm?id=#URLEncodedFormat(Trim

(kid))#&

oid=#URL.oid#&aid=#url.aid#=URL.aid#&at=#approver_type#">#title#</a>
<input type="hidden" name="htitle_#rCount#" value="#kid#" ></td><td

class="#class#"><select name="rOffice_#rCount#" class="KC_office">
      <option value="XO">XO</Option>
      "      "      "      "
      "      "      "      "
      </option>
</select></td>

<td class="#class#"><select name="rank_#rCount#" class="KC_office">
      <option value="1">1</Option>
      "      "      "      "
      "      "      "      "
      </option>
      </select></td>

</tr>
</cfoutput>
0
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
try givving the diff name to
<cfset hID = Evaluate(hID)>

amy be this
<cfset hID2 = Evaluate(hID)>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
Shwapy,

You could greatly simplify the code by just using the cfquery #CurrentRow# variable to generate the field names.  Also, by eliminating Evaluate() which is not needed.

FORM:
<cfoutput query="combine">

   <a href="update_kc.cfm?id=#URLEncodedFormat(Trim(kc_highlights_id))#&oid=#URL.oid#&aid=#url.aid#=URL.aid#&at=#approver_type#">#title#</a>
   <input type="hidden" name="htitle_#currentRow#" value="#kc_highlights_id#" >
   <select name="rOffice_#CurrentRow#" class="KC_office">
      ...options ...
   </select>

   <select name="rank_#CurrentRow#" class="KC_office">
      ...options ...
   </select>

</cfoutput>
<!--- store the total fields in a hidden field --->
<cfoutput>
  <input type="hidden" name="totalFields" value=#combine.recordCount#">
</cfoutput>


Then use the hidden field to do your loop on the action page.  No need for Evaluate(..) at all. Just use array notation to grab the values.


ACTION PAGE:
<cfparam name="form.totalFields" default="0">

<cfloop from="1" index="#form.totalFields#" index="x">
        <cfset hID = form["htitle_"& x]>
        <cfset rOffice = form["roffice_"& x]>
        <cfset Rank = form["rank_"& x]>
        <!--- use CFQUERYPARAM here --->
      <cfquery name=insert_highlights datasource=#DS#>
           Insert into #kcr# (highlight, office, rank)
           Values ( #hID# , #roffice#, #rank# )
      </cfquery>
</cfloop>
 
0
 

Author Comment

by:Shwapy
Comment Utility
Thanks aqx and to everyone else.  I will give this a try and let you know how it works out.
0
 
LVL 19

Expert Comment

by:erikTsomik
Comment Utility
and also please work on your programming technics, make the code is readable and maintenable as possible
0
 

Author Comment

by:Shwapy
Comment Utility
Will do erikTsomik thank you for all of your advise.  I'm learning everyday.

0
 

Author Closing Comment

by:Shwapy
Comment Utility
Thanks for your help!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now