[Webinar] Streamline your web hosting managementRegister Today

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

I am having a problem with varable undefined error

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
Shwapy
Asked:
Shwapy
  • 8
  • 5
  • 4
  • +1
1 Solution
 
erikTsomikSystem Architect, CF programmer Commented:
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
 
MartinCMSCommented:
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
 
erikTsomikSystem Architect, CF programmer Commented:
good catch it should be
<cfset hID = #Evaluate(hID)#>
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
_agx_Commented:
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
 
_agx_Commented:
> 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
 
MartinCMSCommented:
Thanks erikTsomik for the correction!
0
 
ShwapyAuthor Commented:
Thanks guys I will post the form code...
0
 
_agx_Commented:
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
 
ShwapyAuthor Commented:
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
 
erikTsomikSystem Architect, CF programmer Commented:
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
 
ShwapyAuthor Commented:
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
 
ShwapyAuthor Commented:
<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
 
ShwapyAuthor Commented:

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
 
erikTsomikSystem Architect, CF programmer Commented:
try givving the diff name to
<cfset hID = Evaluate(hID)>

amy be this
<cfset hID2 = Evaluate(hID)>
0
 
_agx_Commented:
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
 
ShwapyAuthor Commented:
Thanks aqx and to everyone else.  I will give this a try and let you know how it works out.
0
 
erikTsomikSystem Architect, CF programmer Commented:
and also please work on your programming technics, make the code is readable and maintenable as possible
0
 
ShwapyAuthor Commented:
Will do erikTsomik thank you for all of your advise.  I'm learning everyday.

0
 
ShwapyAuthor Commented:
Thanks for your help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now