0

Delete part of the URL string using removeChars with ColdFusion


While I was doing the final project for my ColdFusion class today, I got one problem which the URL query keeps adding up when I was using CGI.QUERY_STRING. For example, the query string becomes “projectid=5&clear=yes” when I just added the “&clear=yes” to the end of the CGI.QUERY_STRING variable. After the page have been refreshed, the query would become “projectid=5&clear=yes&clear=yes”. This became a problem to me because not only the URL query was not right, but the result of the page was also not right.

After numerous trail of using cfif, I remembered that I could use a build-in ColdFusion function to remove the last part of URL query and store it to a variable to use it later. The name of the function is removeChars. It needs three parameters, a string, the position number of where to begin removing characters, and the position number of where to end the removing of characters. For example, I used the following statement to remove the “&clear=yes” characters at the end of the CGI.QUERY_STRING variable and stores it to a new variable called variables.QUERY_STRING:

<cfset variables.QUERY_STRING = removeChars("#CGI.QUERY_STRING#",12,11)>

Of course I would add a cfif statement to avoid continue removing the characters if there is no “clear=yes” exists in the string:

<cfif CGI.QUERY_STRING CONTAINS "Clear=yes">
 <cfset variables.QUERY_STRING = removeChars("#CGI.QUERY_STRING#",12,11)>
 <cfset variables.clear = "#CGI.QUERY_STRING#">
<cfelse>
 <cfset variables.QUERY_STRING = "#CGI.QUERY_STRING#">
 <cfset variables.clear = "#CGI.QUERY_STRING#&Clear=yes">
</cfif>
 <a href="?#variables.QUERY_STRING#" title="Continue Shopping">Continue Shopping</a><br />
 <a href="?#variables.clear#" title="Clear Cart">Clear Cart</a>

You can see result of the above code here along with my final project.

There are no posts related to Delete part of the URL string using removeChars with ColdFusion.