1

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?

Miguel-F
  • 13,450
  • 6
  • 38
  • 63
jzamora1229
  • 31
  • 1
  • 1
  • 6
  • 2
    Why the cfscript limitation? You are obviously using tags (cfinput) so just use tags and make the distinction when you are building the form. – Miguel-F Mar 17 '15 at 20:32
  • I'm not sure what exactly you mean. I'm open to any ideas to accomplish this. It's a form that they will initially fill out, then click save, and have an option to edit in the future. I would like the fields to be enabled when they are blank, and disabled when populated from the db, and there also be an edit button to enable if anything needs updated. – jzamora1229 Mar 18 '15 at 12:41
  • 2
    Wouldn't the column exist if it's populated or not? Shouldn't you be checking if the column has a value? – Adrian J. Moreno Mar 18 '15 at 15:23
  • Yes! That must be my problem! How do i check for a value? I have a query at the top of my page SELECT * FROM table WHERE this = #var# – jzamora1229 Mar 18 '15 at 15:45
  • 1
    *WHERE this = #var#* That is vulnerable to sql injection. Be sure to use `cfqueryparam`. – Leigh Mar 18 '15 at 16:48

2 Answers2

2

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.

Miguel-F
  • 13,450
  • 6
  • 38
  • 63
  • That makes perfect sense! However, when I do this, for whatever reason, whether it is defined or not, the form only wants to display the first cfinput tag. So either way, the field is disabled, blank or not blank. Any ideas? :/ – jzamora1229 Mar 18 '15 at 15:44
  • Yes, of course, you have to check for whatever value is actually in the database column. I was just going by what you shared in your original question. Glad it is working for you. – Miguel-F Mar 18 '15 at 16:33
  • If a raw `input` is used, then it is disabled using the literal `"disabled"` inline, without assignment or anything: ``. This doesn't work with `cfinput`. I've been experimenting with assigning enabled/disabled true/false, and with the notation above `disabled="disabled"` and `enabled="enabled"`. Is this notation correct, it seems strange? – samus Jan 04 '16 at 21:16
  • For `cfinput` the attribute behavior depends on the format of the form as follows: **HTML format forms:** _ColdFusion passes this attribute directly to the HTML. To disable input, specify disabled without a value (HTML format) or with the value disabled (XHTML compliant). To enable input, omit this attribute._ **Flash format forms:** _To disable input, specify disabled without an attribute, or disabled="yes" (or any ColdFusion positive Boolean value, such as true). To enable input, omit the attribute or specify disabled="no" (or any ColdFusion negative Boolean value, such as false)._ – Miguel-F Jan 05 '16 at 12:50
1

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!

jzamora1229
  • 31
  • 1
  • 1
  • 6
  • 2
    If you are not using any of the extra features, it is better to use a standard html `` instead. The CF wrappers are a bit clunky and add a lot of unnecessary clutter to the html source. Also 1) `NULL` values in a result set are treated as an empty string in CF. Unless you are testing for the literal characters "NULL", you only need the latter check. 2) No need for the # signs in the CFIF. – Leigh Mar 18 '15 at 16:45