Shaun Mccran

My digital playground

03
A
U
G
2009

Writing back to a multi select box using javascript

During the building of an online form I discovered that there was the need for a user to select multiple values from a select form field. This is done easily enough using multiple="true". What I then found was that I wanted them to be able to add more options to the select field, preferably without reloading the entire template. These options would then also be 'selected' on being added to the select box. Below is a test script demonstrating this.

Initially I created a standard select form field, and populate it with a simulated query.

view plain print about
1<!--- Create a test query. --->
2<cfset variables.qOptions = QueryNew( "id, name" ) />
3
4<cfset QueryAddRow( variables.qOptions ) />
5<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "1" />
6<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 1" />
7
8<cfset QueryAddRow( variables.qOptions ) />
9<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "2" />
10<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 2" />
11
12<cfset QueryAddRow( variables.qOptions ) />
13<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "3" />
14<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 3" />
15
16<cfset QueryAddRow( variables.qOptions ) />
17<cfset variables.qOptions[ "id" ][ variables.qOptions.RecordCount ] = "4" />
18<cfset variables.qOptions[ "name" ][ variables.qOptions.RecordCount ] = "Value 4" />
19<select name="item" id="item" class="input_text" size="4" multiple="true">
20<cfloop query="variables.qOptions">
21        <option value="#variables.qOptions.id#">#variables.qOptions.name#</option>
22    </cfloop>
23</select>

Next we will add the form button that allows the user to add new values to the select field. The 'Add' button invokes the javascript function AddItem().

view plain print about
1<input type="text" name="newItem" id="newItem" size="30"> <input type="button" value="Add" onclick="AddItem();">

So a user enters their new item into the text box, and it is inserted into the select field drop down, and automatically selected.

view plain print about
1function AddItem(){
2
3                        var newItemValue = document.getElementById("newItem").value;
4                        var list = document.getElementById('item');
5                        var startNewNum = document.getElementById('item').options.length;
6
7                        var newOption = new Option((newItemValue), newItemValue);
8
9                        // do it if there is a value
10                        if(newItemValue.length !== 0) {
11
12                            newOption.selected = true;
13                            list.options.add(newOption);
14
15                            newItem.value = "";
16                            window.alert('You new value has been added');
17
18                        }
19                    }

The javascript above gets the new item value, and inserts it as the last item in the select list. There is also a check to see if there is any text value in the form field. Just to round it off I remove the text field value, and alert the user.

There is probably a shinier way of doing this in something like JQuery, but I'm not that up to speed on that. It would be interesting to see an example though. (hint!).

30
J
U
L
2009

Cross site Script hacking using the GET method

I've dealt with Cross Site scripting (XSS) attacks before ( http://www.mccran.co.uk/index.cfm/2009/4/6/Cross-Site-scripting-hack-test-form), so I'm familiar with the principles involved. In this example there is a subtle difference.

In the example above the vulnerability was created by POSTING a text string through the form action. In this example we will examine a similar vulnerability using GET. IE we will simply pass the attacking string through the url of the form, setting the form field value in the traditional 'url?variable=N' way.

To demonstrate this create a simple form:

view plain print about
1<cfparam name="attributes.formValue" default="">
2
3<form>
4
5<input type="text" name="formValue" size="20" value="<cfoutput>#attributes.formValue#</cfoutput>">
6<input type="submit" name="Action" value="Send">
7
8</form>

Call your form in a browser. Now append on the end of that url the text string below.

?attributes.formValue==>"><%2Ftitle><%2Fiframe><%2Fscript><%2Fform><%2Ftd><%2Ftr>
<%2FIfRamE>

Reading through the string you'll notice that it is an Iframe constructor that is calling a url, in this case www.Google.com.

As the url is setting the value of 'attributes.formValue' this will be inserted into the form on the submit action. We are not posting it, so it will not be picked up by any custom POST action code.

One interesting point to mention here is that testing this in IE 8, it will actually be blocked by default, as it has detected that scripts are running over different domains.

So if you are in the habit of writing POST detection scripts, make sure you handle any other submissions as well!

30
J
U
L
2009

Displaying raw html onscreen using the Example html tag

Ever needed to display an example block of HTML code in a web based document or regular web page?

I had just that requirement recently, so thought I'd have a dig around. It is one of the smallest html tags I've found, it is the example tag.

view plain print about
1<xmp> html code</xmp>

It works very much like the 'pre' tag in the way that it displays whatever it contains without any sort of formatting, in its raw state. Except that this also display html, rather than interpreting it, it displays it exactly as is.

Nothing revolutionary, but a nice surprise and handy for code samples etc.

28
J
U
L
2009

An exploration of automated stress testing tools

Recently I was looking at the scalability of a web platform, and had to perform some stress analysis on it to evaluate whether or not it would accommodate the potential user base for a new application.

The idea was to pose the question:

"N percentage of web pages should load in X seconds, with no more than Z percent errors."

[ More ]

_UNKNOWNTRANSLATION_ /