Link to home
Start Free TrialLog in
Avatar of Shwapy
Shwapy

asked on

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

Avatar of erikTsomik
erikTsomik
Flag of United States of America image

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#>
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#)>

good catch it should be
<cfset hID = #Evaluate(hID)#>
Avatar of _agx_
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.
> 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)>
Thanks erikTsomik for the correction!
Avatar of Shwapy
Shwapy

ASKER

Thanks guys I will post the form code...
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#>

Avatar of Shwapy

ASKER

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

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
Avatar of Shwapy

ASKER

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>
Avatar of Shwapy

ASKER

<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>
Avatar of Shwapy

ASKER


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>
try givving the diff name to
<cfset hID = Evaluate(hID)>

amy be this
<cfset hID2 = Evaluate(hID)>
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Shwapy

ASKER

Thanks aqx and to everyone else.  I will give this a try and let you know how it works out.
and also please work on your programming technics, make the code is readable and maintenable as possible
Avatar of Shwapy

ASKER

Will do erikTsomik thank you for all of your advise.  I'm learning everyday.

Avatar of Shwapy

ASKER

Thanks for your help!