Thursday, July 7, 2016

State select html

<select name='State' size='1'>
  <option value='' SELECTED>[State]</option>
  <option value='ZZ'>International</option>
  <option value='AL'>AL-Alabama</option>
  <option value='AK'>AK-Alaska</option>
  <option value='AZ'>AZ-Arizona</option>
  <option value='AR'>AR-Arkansas</option>
  <option value='CA'>CA-California</option>
  <option value='CO'>CO-Colorado</option>
  <option value='CT'>CT-Connecticut</option>
  <option value='DC'>DC-Washington D.C.</option>
  <option value='DE'>DE-Delaware</option>
  <option value='FL'>FL-Florida</option>
  <option value='GA'>GA-Georgia</option>
  <option value='HI'>HI-Hawaii</option>
  <option value='ID'>ID-Idaho</option>
  <option value='IL'>IL-Illinois</option>
  <option value='IN'>IN-Indiana</option>
  <option value='IA'>IA-Iowa</option>
  <option value='KS'>KS-Kansas</option>
  <option value='KY'>KY-Kentucky</option>
  <option value='LA'>LA-Louisiana</option>
  <option value='ME'>ME-Maine</option>
  <option value='MD'>MD-Maryland</option>
  <option value='MA'>MA-Massachusetts</option>
  <option value='MI'>MI-Michigan</option>
  <option value='MN'>MN-Minnesota</option>
  <option value='MS'>MS-Mississippi</option>
  <option value='MO'>MO-Missouri</option>
  <option value='MT'>MT-Montana</option>
  <option value='NE'>NE-Nebraska</option>
  <option value='NV'>NV-Nevada</option>
  <option value='NH'>NH-New Hampshire</option>
  <option value='NJ'>NJ-New Jersey</option>
  <option value='NM'>NM-New Mexico</option>
  <option value='NY'>NY-New York</option>
  <option value='NC'>NC-North Carolina</option>
  <option value='ND'>ND-North Dakota</option>
  <option value='OH'>OH-Ohio</option>
  <option value='OK'>OK-Oklahoma</option>
  <option value='OR'>OR-Oregon</option>
  <option value='PA'>PA-Pennsylvania</option>
  <option value='PR'>PR-Puerto Rico</option>
  <option value='RI'>RI-Rhode Island</option>
  <option value='SC'>SC-South Carolina</option>
  <option value='SD'>SD-South Dakota</option>
  <option value='TN'>TN-Tennessee</option>
  <option value='TX'>TX-Texas</option>
  <option value='UT'>UT-Utah</option>
  <option value='VT'>VT-Vermont</option>
  <option value='VA'>VA-Virginia</option>
  <option value='WA'>WA-Washington</option>
  <option value='WV'>WV-West Virginia</option>
  <option value='WI'>WI-Wisconsin</option>
  <option value='WY'>WY-Wyoming</option>
</select>

Friday, January 22, 2016

Dynamically update multiple rows in one form

This is one of those functions that I have avoided for a long time.

I decided that is was time for me to tackle it so here goes.

Scenario:

You have a form with multiple rows.

You need to perform CRUD on the records all in one place. (Brain starting to hurt yet?)

Don't worry. It's fairly easy.

Step 1 - Retrieve your records:

<cfquery name="getArt" datasource="cfartgallery" maxrows="10"> 
select artid, artname from art 
</cfquery>

Step 2 - Build Your Form.

#currentRow# is used to give each row a unique identifier and I pass the row count as a hidden field to use to loop through the fields once submitted (see red highlights below).

<form action="formLoopUpdate.cfm" method="post">
  <table>
    <cfloop query="getArt">
      <tr> <cfoutput>
          <td>
          <input type="text" name="art#currentRow#" value="#artname#" />
          <input type="hidden" name="artID#currentRow#" value="#artID#" />
          </td>
        </cfoutput> </tr>
    </cfloop>
  </table>
  
  <input type='hidden' name='totalNumberOfRecords' value='<cfoutput>#getArt.recordCount#</cfoutput>'>
  
  <input type="submit" name="save" value="Save" />
</form>


3. Submit and save

No need for commentary here. It's explains itself.

<cfif structKeyExists(form, "save")>
   
        <cfloop from="1" to="#form.totalNumberOfRecords#" index="counter">
          <!--- extract the values of the dynamic form fields --->
          <cfset art   = form["art"   & counter]>
          <cfset artID   = form["artID"   & counter]>
    
            <cfquery datasource="cfartgallery"> 
                    update art 
                    set artname = <cfqueryparam cfsqltype="cf_sql_varchar" value="#art#"> 
                    where artid = <cfqueryparam cfsqltype="cf_sql_integer" value="#artid#"> 
            </cfquery> 
    
         </cfloop>
    
    
    </cfif>


That's it.

Let me know what you think or how this could have been done differently.

-MB



Another example:



<cfloop from="1" to="#form.totalNumberItems#" index="counter">


<!--- update category order --->

<cfif isdefined("form.categoryOrder#counter#")> 


<cfset categoryOrder = form["categoryOrder"   & counter]>

<cfset categoryID   = form["categoryID"   & counter]>


<cfquery name="updateCategoryOrder" datasource="#application.var#"> 

update Checklist_Category 

set sortOrder = <cfqueryparam cfsqltype="cf_sql_integer" value="#categoryOrder#"> 

where categoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#categoryID#"> 

</cfquery>  


</cfif>    



<!--- update checklist item order --->

<cfif isdefined("form.checkListOrder#counter#")> 


<cfset checkListOrder = form["checkListOrder"   & counter]>

<cfset checkDetailID    = form["checkDetailID"    & counter]>


<cfquery name="updateCheckListOrder" datasource="#application.var#"> 

update Checklist_Admin 

set sortOrder = <cfqueryparam cfsqltype="cf_sql_integer" value="#checkListOrder#"> 

where checkDetailID = <cfqueryparam cfsqltype="cf_sql_integer" value="#checkDetailID#"> 

</cfquery> 


</cfif>

</cfloop>


</cfif>



Tuesday, October 6, 2015

Remove Trailing Characters with Javascript

This is not CF but it sure comes in handy.

If for any reason you need to get rid pesky trailing characters, here's the script:

<script>
function trimTrailingChars(s, charToTrim)
   {
     var regExp = new RegExp(charToTrim + "+$");
     var result = s.replace(regExp, "");

     return result;
   }
</script>




And here is how you use it in your code:

var sText = trimTrailingChars(sText, ',');


Note the red text above. Change the comma to whatever you want to get rid of.


Friday, March 13, 2015

Customized placement of jQuery error messages

The code is pretty simple. I have highlighted the most important parts in red.




<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
  $(function() {
    $( "#DateChooser" ).datepicker();
  });
  </script>
<style class="cp-pen-styles">
.error {font-size:12px; color:#CC0000;}
</style>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
$("#form").validate({
    messages: {
        name: "Please enter name.",
        surname: "Please enter surname.",  
DateChooser: "Please enter date."
    },
    errorElement: "div",   
    errorPlacement: function(error, element) {
            error.insertAfter(element);
        }   
});
});
});//]]>

</script>
</head>
<body>
<cfform id="form">
  <label for="name">Name</label>
  <input type="text" name="name" id="name" class="required" minlength="2" />
  <br/>
  <div></div>
  <br>
  <label for="DateChooser">Date</label>
  <input   id="DateChooser" class="required"  name="DateChooser">
  <br/>
  <div></div>
  <br>
  <label for="surname">Surname</label>
  <input type="text" name="surname" id="surname" class="required" minlength="2" />
  <br />
  <div></div>
  <input type="submit" value="submit" id="submit" />
</cfform>
</body>
</html>

Thursday, March 12, 2015

Easy jQuery Error Message Customization

This code will allow you to customize your jQuery error messages:


Add these links to your page

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js'></script>
<script src='https://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js'></script>



You can style your error messages:

<style class="cp-pen-styles">
.error {
color:red;
font-family:verdana, Helvetica;
}
</style>



Your script


<script>
$(function () {
    $('#formTest').validate({
        rules: {
            name: { required: true },
address: { required: true }

        },
        messages: {
            name: { required: 'Please enter your name' },
address: { required: 'Please enter your address' }
        }
    });


});
  </script>

Your form

<form action='test.cfm' id="formTest" name="formTest" method='POST'>
  <input name="name" value="" />
  <input name="address" value="" />
  <input type="submit" class="submit" value="Add Appeal Info">
</form>

Thursday, January 29, 2015

How to find spaces in a form field in Coldfusion

This is pretty straight forward

<cfset strTest = "#form.variable#">
<cfset strTarget = " ">
<cfset newTest=Chr(1) & strTest & Chr(1)>
<cfset intCount=DecrementValue(ArrayLen(newTest.split(strTarget.replaceAll("(\W)","\\$1"))))>
<cfoutput>#intCount#</cfoutput>

This will count the number of spaces in the variable.

If the intCount = 0, you are good to go.

You can use the code to test for any character.

Enjoy!

Thursday, December 18, 2014

How to dynamically define quarterly dates in Coldfusion

You know those reports that upper management request from time to time? The ones that give you nightmares? Yeah those. Well sometimes they will request quarterly reports so I wanted to share an easy way to define quarterly dates.

So we start with our default variables:

<cfset quarter = 1>
<cfset year = "#year(now())#">
<cfset startmonth = 1>

And here are the functions that create the dates:

<cffunction name="QuarterFirstDate" returnType="date">
    <cfargument name="quarternumber" required="yes" type="numeric">
    <cfargument name="yr" type="numeric" default="#year(now())#">
    <cfargument name="startmonth" type="numeric" default="1">
    <cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
    <cfreturn firstDate>
</cffunction>

<cffunction name="QuarterLastDate" returnType="date">
    <cfargument name="quarternumber" required="yes" type="numeric">
    <cfargument name="yr" type="numeric"  default="#year(now())#">
    <cfargument name="startmonth" type="numeric" default="1">
    <cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
    <cfreturn lastDate>
</cffunction>


And here is how I define my returned variables to be used in my code:

<cfset startDate = #DateFormat(QuarterFirstDate(quarter, year, startmonth))#>
<cfset endDate = #DateFormat(QuarterLastDate(quarter, year, startmonth))#>

To change the quarters, simply change the default quarter variable define at the top of the page:

<cfset quarter = 1> = 01-Jan-14 - 31-Mar-14
<cfset quarter = 2>  = 01-Apr-14 - 30-Jun-14
<cfset quarter = 3>  = 01-Jul-14 - 30-Sep-14
<cfset quarter = 4>  01-Oct-14 - 31-Dec-14


nJoy!