Skip to main content
May 8, 2012
Answered

Element is undefined in url

  • May 8, 2012
  • 2 replies
  • 3612 views

Hi all

The cfif doesn't appear to be working, if I add sectorid=1 to the url it works but throws an error if there is no value. Why is this not working? Thank you

<cfquery name="qNames" datasource="sales">

    select * from COMPANY, sector

where COMPANY.sectorid = sector.sectorid

<cFIF IsDefined(url.sectorID)>

and sector.sectorid = #url.sectorid#

</cfif>

</cfquery>

This topic has been closed for replies.
Correct answer Adam Cameron.

Hi thanks for the replies I used structKeyExists as per lyndon's recommendation, but the page throws an error if there is page.cfm without any sectorid parameter

<cfinclude template = "header.cfm">

<cfquery name="qNames" datasource="sales">
    select * from COMPANY, sector
where COMPANY.sectorid = sector.sectorid
<cfif structKeyExists(url,"sectorID")>
and sector.sectorid = #url.sectorid#
</cfif>
</cfquery>

<cfquery name="sNames" datasource="sales">
    select * from COMPANY, sector
where <cFIF IsDefined('url.sectorID')>
sector.sectorid = #url.sectorid#
</cfif>
</cfquery>


<CFQUERY name="sector" datasource="sales">
SELECT sectorId, sector
FROM sector
ORDER BY sector
</CFQUERY>


but the page throws an error if there is page.cfm without any sectorid parameter

If you ever get an error you want help with, don't just tell us you got an error, tell us what the error is.  Inded paying close attention to the error message will generally tell you where you're going wrong.

Also, look at this:

<cfquery name="sNames" datasource="sales">
    select * from COMPANY, sector
where <cFIF IsDefined('url.sectorID')>
sector.sectorid = #url.sectorid#
</cfif>
</cfquery>

If there is no sectorid, what SQL will that create?

The SQL being sent to the DB should be part of the error message, btw...

--
Adam

2 replies

Inspiring
May 8, 2012

You're not using isDefined() properly.

Read the docs:

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fbd.html

And pay close attention to the example code.

Also: you're better off using structKeyExists() than isDefined(): isDefined() is flaky sometimes, and is kind of a hangover from older versions of CF.

--

Adam

Inspiring
May 8, 2012

Lyndon Patton

405-301-0952

www.ohrc.org

iPhone

May 8, 2012

hi lyndon you kind reply was empty!

Inspiring
May 8, 2012

<cFIF IsDefined("url.sectorID")>