Is it possible to disable a cfinput type=text field using a conditional cfscript if statement?

1.7k Views Asked by At

I have a cfform with dynamically populated text fields. This is all inside of a logged in portal. If the database is empty when the user arrives at the form, I would like the fields to be "enabled" so they may fill out the information and submit the form. However, if they have already done so previously and the database is populated, I would like the fields to be "disabled".

Essentially, I'm wanting:

<cfscript>
if (isDefined("query.column"))
{
disable the cfinput fields
}

Is this possible? If not, any ideas on how to accomplish this?

2

There are 2 best solutions below

4
On

Here is what I meant from my comment. I don't believe there is any reason to do what you want within cfscript tags. You are already using the tag syntax for your form so just check your condition(s) when you are building the form.

For example, now you have something like this:

<html>
    <head>
        <!-- some code here -->
    </head>
    <body>
        <!-- some code here -->

        <cfform ... >
            <cfinput type="text" ... >
        </cfform>

        <!-- some code here -->
    </body>
</html>

What I am suggesting is to do something like this:

<html>
    <head>
        <!-- some code here -->
    </head>
    <body>
        <!-- some code here -->

        <cfform ... >
            <cfif isDefined("query.column")>
                <cfinput type="text" disabled="disabled" ... >
            <cfelse>
                <cfinput type="text" enabled="enabled" ... >
            </cfif>
        </cfform>

        <!-- some code here -->
    </body>
</html>

Use similar conditions in the tags as you build the form for any other fields or buttons etc.

1
On

I figured out the problem. I did have to check for a value. I re-wrote the cfif as

<cfif #query.column# eq "NULL" OR #query.column# eq "">
    <cfinput type="text">
<cfelse>
    <cfinput type="text" value="#query.column#" disabled="disabled">
</cfif>

Thank you guys for your help!