Skip to main content
Known Participant
January 6, 2011
Question

Form Field Appear on Value Entered

  • January 6, 2011
  • 2 replies
  • 1756 views

I am running Coldfusion 9 and building a website using Dreamweaver from CS5.

Is there a way to make a form field appear only if a certain value was entered into the previous form field? If so how? For example I am creating a registration page and one of the form fields is "Property Type" where the user can choose from "Residential, Commercial, Other" from a drop down menu and if the user chooses "Commercial" I want an additional field to pop up for the user to enter a value in "Company Name"??? Thanks for your help

    This topic has been closed for replies.

    2 replies

    WolfShade
    Legend
    January 6, 2011

    szafranman wrote:

    I am running Coldfusion 9 and building a website using Dreamweaver from CS5.

    Is there a way to make a form field appear only if a certain value was entered into the previous form field? If so how? For example I am creating a registration page and one of the form fields is "Property Type" where the user can choose from "Residential, Commercial, Other" from a drop down menu and if the user chooses "Commercial" I want an additional field to pop up for the user to enter a value in "Company Name"??? Thanks for your help

    Totally the wrong forum for it, but I'll try to help.

    Initial page load, the text field should load with a style="display:none;" parameter.

    For the drop down, add onChange="showHideInput(this.options[this.selectedIndex].value,' the name of the hidden text field ');"

    Create a <script type="text/javascript"></script> in the document head.

    Inside the script tags, the following:

    function showHideInput(ddValue,txtFld) {

      if(toLowerCase(ddValue) == "commercial") {

         txtFld.style.display = '';

         }

      else {

         txtFld.style.display = 'none';

         txtFld.value = '';

        }

    }

    That should do it.

    ^_^

    Known Participant
    January 6, 2011

    WolfShade,

    My apologizes for posting this question in the wrong forum. Since I have already started the discussion here I will ask you my question here still. I put in the lines of code you suggested however when I go to the drop down list and select "Commercial" no text input box appears for the Company Name field. If you have a min I will post my code below. Your help is very much appreciated. I should let you know I also tried adding the on change to the actual option tag for commerical i.e <option onChange...>Commercial</option> and still did not appear. I put the work area in red text hope it helps.

    <cfif IsDefined("FORM.username")>

      <cfquery name="MM_search" datasource="Access">

        SELECT Logininfo.Username FROM Logininfo WHERE Logininfo.Username=<cfqueryparam value="#FORM.username#" cfsqltype="cf_sql_clob" maxlength="50">

      </cfquery>

      <cfif MM_search.RecordCount GTE 1>

        <cflocation url="taken.cfm?requsername=#FORM.username#" addtoken="no">

      </cfif>

    </cfif>

    <cfset CurrentPage=GetFileFromPath(GetBaseTemplatePath())>

    <cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "customer">

      <cfif IsDefined('form.propertytype') and len(form.propertytype) NEQ 0>

      <cfset #CustomerTypeRefFullName#=form.propertytype/>

      </cfif>

      <cfif IsDefined('form.companyname') and len(form.companyname) NEQ 0>

      <cfset #CompanyName#=form.companyname/>

      </cfif>

      <cfif IsDefined('form.salutation') and len(form.salutation) NEQ 0>

      <cfset #Salutation#=form.salutation/>

      </cfif>

      <cfif IsDefined('form.firstname') and len(form.firstname) NEQ 0>

    <cfset #FirstName#=form.firstname/>

      </cfif>

        <cfif IsDefined('form.lastname') and len(form.lastname) NEQ 0>

        <cfset #LastName#=form.lastname/>

        </cfif>

      <cfset Name=#FirstName#&''&#LastName#/>

      <cfset BillAddressAddr1=#Salutation#&' '&#FirstName#&''&#LastName#/>

        <cfif IsDefined('form.streetaddress') and len(form.streetaddress) NEQ 0>

        <cfset #BillAddressAddr2#=form.streetaddress/>

      </cfif>

        <cfif IsDefined('form.city') and len(form.city) NEQ 0>

        <cfset #BillAddressCity#=form.city/>

        </cfif>

        <cfif IsDefined('form.state') and len(form.state) NEQ 0>

        <cfset #BillAddressState#=form.state/>

        </cfif>

      <cfif IsDefined('form.zipcode') and len(form.zipcode) NEQ 0>

        <cfset #BillAddressPostalCode#=form.zipcode/>

        </cfif>

    <cfset Contact=#FirstName#&''&#LastName#/>

      <cfif IsDefined('form.dayphone') and len(form.dayphone) NEQ 0>

        <cfset #Phone#=form.dayphone/>

      </cfif>

      <cfif IsDefined('form.eveningphone') and len(form.eveningphone) NEQ 0>

        <cfset #AltPhone#=form.eveningphone/>

      </cfif>

      <cfif IsDefined('form.email') and len(form.email) NEQ 0>

        <cfset #Email#=form.email/>

      </cfif>

    <cfquery datasource="QBs">  

        INSERT INTO Customer (CustomerTypeRefFullName, CompanyName, Salutation, FirstName, LastName, Name, BillAddressAddr1, BillAddressAddr2, BillAddressCity, BillAddressState, BillAddressPostalCode, Contact, Phone, AltPhone, Email)

    VALUES (

    <cfqueryparam value="#CustomerTypeRefFullName#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#CompanyName#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#Salutation#" cfsqltype="cf_sql_varchar" maxlength="15"/>,

    <cfqueryparam value="#FirstName#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#LastName#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#Name#" cfsqltype="cf_sql_varchar" maxlength="50"/>,

    <cfqueryparam value="#BillAddressAddr1#" cfsqltype="cf_sql_varchar" maxlength="50"/>,

    <cfqueryparam value="#BillAddressAddr2#" cfsqltype="cf_sql_varchar" maxlength="50"/>,

    <cfqueryparam value="#BillAddressCity#" cfsqltype="cf_sql_varchar" maxlength="50"/>,

    <cfqueryparam value="#BillAddressState#" cfsqltype="cf_sql_varchar" maxlength="25"/>,

    <cfqueryparam value="#BillAddressPostalCode#" cfsqltype="cf_sql_varchar" maxlength="13"/>,

    <cfqueryparam value="#Contact#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#Phone#" cfsqltype="cf_sql_varchar" maxlength="15"/>,

    <cfqueryparam value="#AltPhone#" cfsqltype="cf_sql_varchar" maxlength="15"/>,

    <cfqueryparam value="#Email#" cfsqltype="cf_sql_carchar" maxlength="75"/>

    )

      </cfquery>

    <cfif IsDefined('form.firstname') and len(form.firstname) NEQ 0>

    <cfset #FirstName#=form.firstname/>

    </cfif>

    <cfif IsDefined('form.lastname') and len(form.lastname) NEQ 0>

    <cfset #LastName#=form.lastname/>

    </cfif>

    <cfif IsDefined('form.username') and len(form.username) NEQ 0>

    <cfset #Username#=form.username/>

    </cfif>

    <cfif IsDefined('form.password') and len(form.password) NEQ 0>

    <cfset #Password#=form.password/>

    </cfif>

    <cfquery datasource="Access">

    INSERT INTO Logininfo (FirstName, LastName, Username, Password)

    VALUES (

    <cfqueryparam value="#FirstName#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#LastName#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#Username#" cfsqltype="cf_sql_varchar" maxlength="41"/>,

    <cfqueryparam value="#Password#" cfsqltype="cf_sql_varchar" maxlength="41"/>

    )

    </cfquery>

      <cflocation url="login.cfm">

    </cfif>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html><!-- InstanceBegin template="/Templates/Main.dwt" codeOutsideHTMLIsLocked="false" -->

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <!-- InstanceBeginEditable name="title" -->

    <title>TS Landscaping LLC</title>

    <!-- InstanceEndEditable -->

    <link href="styles/Main.css" rel="stylesheet" type="text/css">

    <script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>

    <link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css">

    <!-- InstanceBeginEditable name="head" -->

    <script type="text/javascript">function showHideInput(ddValue,txtFld) {

      if(toLowerCase(ddValue) == "commercial") {

         txtFld.style.display = '';

         }

      else {

         txtFld.style.display = 'none';

         txtFld.value = '';

        }

    }

    </script>

    <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>

    <script src="SpryAssets/SpryValidationPassword.js" type="text/javascript"></script>

    <script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>

    <script src="SpryAssets/SpryValidationConfirm.js" type="text/javascript"></script>

    <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />

    <link href="SpryAssets/SpryValidationPassword.css" rel="stylesheet" type="text/css" />

    <link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css">

    <link href="SpryAssets/SpryValidationConfirm.css" rel="stylesheet" type="text/css">

    <!-- InstanceEndEditable --></head>

    <body>

    <div id="Container">

      <div id="header"><img src="Images/Website-Image.gif" width="393" height="217" alt="TS Landscaping Logo and Name of company"></div>

      <div id="menu_bar1">

        <ul id="MenuBar1" class="MenuBarHorizontal">

          <li><a class="MenuBarItemSubmenu" href="index.html">Home</a>

            <ul>

              <li><a href="#">Item 1.1</a></li>

              <li><a href="#">Item 1.2</a></li>

              <li><a href="#">Item 1.3</a></li>

            </ul>

          </li>

          <li><a href="about_us.html">About Us</a></li>

          <li><a href="#">Services</a></li>

          <li><a class="MenuBarItemSubmenu" href="#">User Account</a>

            <ul>

              <li><a href="login.cfm">Login</a></li>

              <li><a href="registration_page.cfm">Register</a></li>

              <li><a href="members_page.cfm">Customer Page</a></li>

            </ul>

          </li>

          <li><a href="contact_us.html">Contact Us</a></li>

        </ul>

      </div>

      <div id="Main">

        <div id="side_bar"><!-- InstanceBeginEditable name="sidebar" -->Content for  id "side_bar" Goes Here<!-- InstanceEndEditable --></div>

        <div id="main_body"><!-- InstanceBeginEditable name="mainbody" -->

    <h1>Welcome to our sign up Page!</h1>

    <p>Please fill out the form below to register with out site and gain access to our members account page.</p>

    <form name="customer" action="<cfoutput>#CurrentPage#</cfoutput>" method="POST" id="customer"><table width="auto" border="0" align="center">

      <tr>

    <td><label for="propertytype">

      <div align="right">Property Type:</div>

    </label></td>

        <td><select onChange="showHideInput(this.options[this.selectedIndex].value,'companyname');" name="propertytype" id="propertytype" accesskey="n" tabindex="02">

          <option selected>Residential</option>

          <option >Commercial</option>

          <option>Other</option>

        </select></td>

      </tr>

      <tr>

      <td><label for="companyname">

        <div align="right">Company Name:</div>

      </label></td>

        <td><span id="sprytextfield11">

          <input type="text" name="companyname" id="companyname" accesskey="n" tabindex="03" style="display:none;">

    </span></td>

       </tr>

      <tr>

        <td><label for="salutation">

          <div align="right">Salutation:

            </label>

          </div></td>

        <td><select name="salutation" id="salutation" accesskey="n" tabindex="04">

          <option selected>Mr.</option>

          <option>Mrs.</option>

          <option>Ms.</option>

        </select></td>

      </tr>

      <tr>

        <td><label for="firstname">

          <div align="right">First Name:

            </label>

          </div></td>

        <td><span id="sprytextfield1">

          <input type="text" name="firstname" id="firstname" accesskey="n" tabindex="05" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="lastname">

          <div align="right">Last Name:

            </label>

          </div></td>

        <td><span id="sprytextfield2">

          <input type="text" name="lastname" id="lastname" accesskey="n" tabindex="10" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="streetaddress">

          <div align="right">Street Address

            </label>

          </div></td>

        <td><span id="sprytextfield3">

          <input type="text" name="streetaddress" id="streetaddress" accesskey="n" tabindex="15" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="city">

          <div align="right">City:

            </label>

          </div></td>

        <td><span id="sprytextfield4">

          <input type="text" name="city" id="city" accesskey="n" tabindex="20" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="state">

          <div align="right">State:

            </label>

          </div></td>

        <td><span id="sprytextfield5">

          <input type="text" name="state" id="state" accesskey="n" tabindex="25" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="zipcode">

          <div align="right">Zipcode:

            </label>

          </div></td>

        <td><span id="sprytextfield6">

          <input type="text" name="zipcode" id="zipcode" accesskey="n" tabindex="30" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

        <tr>

        <td><label for="dayphone">

          <div align="right">Day Phone:</div>

        </label></td>

        <td><span id="sprytextfield8">

          <input type="text" name="dayphone" id="dayphone" accesskey="n" tabindex="31">

    </span></td>

        </tr>

          <tr>

        <td><label for="eveningphone">

          <div align="right">Evening Phone:</div>

        </label></td>

        <td><span id="sprytextfield9">

          <input type="text" name="eveningphone" id="eveningphone" accesskey="n" tabindex="32">

    </span></td>

        </tr>

          <tr>

        <td><label for="email">

          <div align="right">Email:</div>

        </label></td>

        <td><span id="sprytextfield10">

          <input type="text" name="email" id="email" accesskey="n" tabindex="33">

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

        </tr>

      <tr>

        <td><label for="username">

          <div align="right">Username:

            </label>

          </div></td>

        <td><span id="sprytextfield7">

          <input type="text" name="username" id="username" accesskey="n" tabindex="40" />

          <span class="textfieldRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="password">

          <div align="right">Password:

            </label>

          </div></td>

        <td><span id="sprypassword1">

          <input type="password" name="password" id="password" accesskey="n" tabindex="45" />

          <span class="passwordRequiredMsg">A value is required.</span></span></td>

      </tr>

      <tr>

        <td><label for="passwordconfirm">

          <div align="right">Confirm Password

            </label>

          </div></td>

        <td><span id="spryconfirm1">

          <input type="password" name="passwordconfirm" id="passwordconfirm" accesskey="n" tabindex="50">

          <span class="confirmRequiredMsg">A value is required.</span><span class="confirmInvalidMsg">The values don't match.</span></span></td>

      </tr>

      <tr>

        <td colspan="2"><div align="center">

          <input type="submit" name="submit" id="submit" value="Register" accesskey="n" tabindex="50" />

        </div></td>

        </tr>

    </table>

      <input type="hidden" name="MM_InsertRecord" value="customer" />

    </form>

    <script type="text/javascript">

    var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");

    var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");

    var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");

    var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");

    var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5");

    var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6");

    var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7");

    var sprypassword1 = new Spry.Widget.ValidationPassword("sprypassword1");

    var spryconfirm1 = new Spry.Widget.ValidationConfirm("spryconfirm1", "password");

    var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8", "none", {isRequired:false});

    var sprytextfield9 = new Spry.Widget.ValidationTextField("sprytextfield9", "none", {isRequired:false});

    var sprytextfield10 = new Spry.Widget.ValidationTextField("sprytextfield10");

    var sprytextfield11 = new Spry.Widget.ValidationTextField("sprytextfield11", "none", {isRequired:false});

    </script><!-- InstanceEndEditable --></div>

      </div>

      <div id="footer">

        <p>Home | About Us | Site Map </p>

        <p>&copy; TS Landscaping LLC 2010</p>

      </div>

    </div>

    <script type="text/javascript">

    var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"../SpryAssets/SpryMenuBarDownHover.gif", imgRight:"../SpryAssets/SpryMenuBarRightHover.gif"});

        </script>

    </body>

    <!-- InstanceEnd --></html>

    WolfShade
    Legend
    January 6, 2011

    Sorry.. I forgot one very important element.

    function showHideInput(ddValue,txtFld) {

      if(toLowerCase(ddValue) == "commercial") {

         document[txtFld].style.display = '';

         }

      else {

        document[txtFld].style.display = 'none';

        document[txtFld].value = '';

        }

    }

    That should fix it.

    ^_^

    Owainnorth
    Inspiring
    January 6, 2011

    That'll be Javascript you need amigo, should be fairly simple to do