Whatever message this page gives is out now! Go check it out!
IsDefined(variable)Parameter | Description |
variable | String, enclosed in quotation marks. Name of variable to test for. |
if(isDefined("form.myVariable"))
if(structKeyExists(form,"myVariable"))<cfscript>
myarray=["1","4","23","5","54"];
for(i=1; i <= arrayLen(myarray); i++){
writeoutput(myarray[i] & "<br>");
}
writeOutput(isDefined("myarray"))
</cfscript><cfscript>
// Simple Form Processing with IsDefined
// Business: Contact form with optional fields
// Sample form submissions
forms = [
{
name: "John Smith",
email: "john@company.com",
message: "Interested in your services",
phone: "555-123-4567",
company: "Tech Corp"
},
{
name: "Sarah Wilson",
email: "sarah@startup.com",
message: "Quick question about pricing"
// Missing phone and company
}
];
// Simple form processing function
function processForm(formData) {
var result = "Form Processing Result:<br><br>";
// Check required fields
if (isDefined("formData.name") AND isDefined("formData.email") AND isDefined("formData.message")) {
result &= "✅ <strong>SUCCESS!</strong><br>";
result &= "<strong>Name:</strong> " & formData.name & "<br>";
result &= "<strong>Email:</strong> " & formData.email & "<br>";
result &= "<strong>Message:</strong> " & formData.message & "<br>";
// Check optional fields safely
if (isDefined("formData.phone")) {
result &= "<strong>Phone:</strong> " & formData.phone & "<br>";
} else {
result &= "<strong>Phone:</strong> Not provided<br>";
}
if (isDefined("formData.company")) {
result &= "<strong>Company:</strong> " & formData.company & "<br>";
} else {
result &= "<strong>Company:</strong> Not specified<br>";
}
} else {
result &= "❌ <strong>FAILED - Missing required fields</strong><br>";
}
return result;
}
</cfscript>
<cfoutput>
<h1>📝 IsDefined: Form Processing Demo</h1>
<p><strong>Business Need:</strong> Handle contact forms with optional fields</p>
<p><strong>Challenge:</strong> Some form fields might be missing</p>
<p><strong>Solution:</strong> Use isDefined() to check before accessing</p>
<hr>
<h2>Example 1: Complete Form</h2>
<div style="background: ##d4edda; padding: 15px; border-radius: 5px;">
#processForm(forms[1])#
</div>
<h2>Example 2: Partial Form (Missing Optional Fields)</h2>
<div style="background: ##d4edda; padding: 15px; border-radius: 5px;">
#processForm(forms[2])#
</div>
</cfoutput><cfscript>
// Simple URL Parameter Handling with IsDefined
// Business: Product search with optional filters
// Sample URL parameters (normally from URL scope)
urlParams = [
{
keyword: "laptop"
// Basic search - no filters
},
{
keyword: "laptop",
brand: "dell",
price: "500-1000",
rating: "4"
// Advanced search with filters
}
];
// Simple search function
function buildSearch(params) {
var result = "Search Results:<br><br>";
// Check for required keyword
if (isDefined("params.keyword")) {
result &= "🔍 <strong>Searching for:</strong> " & params.keyword & "<br>";
var filters = [];
// Check optional filters safely
if (isDefined("params.brand")) {
arrayAppend(filters, "Brand: " & params.brand);
}
if (isDefined("params.price")) {
arrayAppend(filters, "Price: $" & params.price);
}
if (isDefined("params.rating")) {
arrayAppend(filters, "Rating: " & params.rating & "+ stars");
}
if (arrayLen(filters) > 0) {
result &= "<strong>Filters:</strong> " & arrayToList(filters, ", ") & "<br>";
} else {
result &= "<strong>Filters:</strong> None applied<br>";
}
result &= "<strong>Status:</strong> ✅ Search ready<br>";
} else {
result &= "❌ <strong>No search keyword provided</strong><br>";
}
return result;
}
</cfscript>
<cfoutput>
<h1>🔗 IsDefined: URL Parameter Demo</h1>
<p><strong>Business Need:</strong> Handle product search with optional filters</p>
<p><strong>Challenge:</strong> URL parameters vary by user and page</p>
<p><strong>Solution:</strong> Use isDefined() to check what's available</p>
<hr>
<h2>Example 1: Basic Search (keyword only)</h2>
<div style="background: ##e3f2fd; padding: 15px; border-radius: 5px;">
#buildSearch(urlParams[1])#
</div>
<h2>Example 2: Advanced Search (with filters)</h2>
<div style="background: ##e3f2fd; padding: 15px; border-radius: 5px;">
#buildSearch(urlParams[2])#
</div>
</cfoutput><cfscript>
// Simple Configuration Management with IsDefined
// Business: Handle optional application settings
// Sample configurations
configs = [
{
appName: "My ColdFusion App",
version: "1.0.0"
// Basic config - missing optional settings
},
{
appName: "My ColdFusion App",
version: "1.0.0",
debugMode: true,
emailEnabled: true,
cacheEnabled: true,
logLevel: "INFO"
// Full config with all optional settings
}
];
// Simple configuration loader
function loadConfig(configData) {
var result = "Configuration Loaded:<br><br>";
// Load basic required settings
if (isDefined("configData.appName")) {
result &= "<strong>App Name:</strong> " & configData.appName & "<br>";
} else {
result &= "<strong>App Name:</strong> Default App<br>";
}
if (isDefined("configData.version")) {
result &= "<strong>Version:</strong> " & configData.version & "<br>";
} else {
result &= "<strong>Version:</strong> 1.0.0<br>";
}
// Check optional features safely
if (isDefined("configData.debugMode")) {
result &= "<strong>Debug Mode:</strong> " & (configData.debugMode ? "Enabled" : "Disabled") & "<br>";
} else {
result &= "<strong>Debug Mode:</strong> Disabled (default)<br>";
}
if (isDefined("configData.emailEnabled")) {
result &= "<strong>Email Service:</strong> " & (configData.emailEnabled ? "Enabled" : "Disabled") & "<br>";
} else {
result &= "<strong>Email Service:</strong> Disabled (default)<br>";
}
if (isDefined("configData.cacheEnabled")) {
result &= "<strong>Cache:</strong> " & (configData.cacheEnabled ? "Enabled" : "Disabled") & "<br>";
} else {
result &= "<strong>Cache:</strong> Disabled (default)<br>";
}
if (isDefined("configData.logLevel")) {
result &= "<strong>Log Level:</strong> " & configData.logLevel & "<br>";
} else {
result &= "<strong>Log Level:</strong> ERROR (default)<br>";
}
result &= "<br>✅ <strong>App started successfully!</strong>";
return result;
}
</cfscript>
<cfoutput>
<h1>⚙️ IsDefined: Configuration Demo</h1>
<p><strong>Business Need:</strong> Handle optional application settings</p>
<p><strong>Challenge:</strong> Different environments have different config completeness</p>
<p><strong>Solution:</strong> Use isDefined() with sensible defaults</p>
<hr>
<h2>Example 1: Basic Configuration</h2>
<div style="background: ##fff3cd; padding: 15px; border-radius: 5px;">
#loadConfig(configs[1])#
</div>
<h2>Example 2: Full Configuration</h2>
<div style="background: ##d1ecf1; padding: 15px; border-radius: 5px;">
#loadConfig(configs[2])#
</div>
</cfoutput>