Solved

I am having a problem with varable undefined error

Posted on 2009-07-08
19
254 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
ID: 24806691
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
ID: 24806711
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
ID: 24806724
good catch it should be
<cfset hID = #Evaluate(hID)#>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24806735
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_
ID: 24806746
> 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
ID: 24806750
Thanks erikTsomik for the correction!
0
 

Author Comment

by:Shwapy
ID: 24806818
Thanks guys I will post the form code...
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24806839
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
ID: 24807194
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 19

Expert Comment

by:erikTsomik
ID: 24807275
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
ID: 24807384
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
ID: 24807406
<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
ID: 24807453

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
ID: 24807474
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
ID: 24807666
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
ID: 24808019
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
ID: 24808064
and also please work on your programming technics, make the code is readable and maintenable as possible
0
 

Author Comment

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

0
 

Author Closing Comment

by:Shwapy
ID: 31601255
Thanks for your help!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

914 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

16 Experts available now in Live!

Get 1:1 Help Now