|
|
Passing data from one form to another
You are here: irt.org | Articles | JavaScript | Form | Passing data from one form to another Published on: Saturday 14th February 1998 By: Martin Webb IntroductionThis article describes how to transfer data from a form on one page to a form on another page. Note: The examples in this article will not work offline in MSIE version 3.x, as it uses the locations search property to pass the form information between pages. Simple FormsText FieldsTo transfer the contents of the following form to the same form on another page is quite simple:
If we enter some information in the text box and then press enter the browser will load the output.htm file into the browser. Unfortunately the form data will not be passed because the text field was not named. This can be easily rectified:
The browser passes the name of the form field along with the value of the field as part of location: http://www.irt.org/articles/js054/output.htm?myfield=abc123 If we include more form fields:
Then the form fields name-value pairs are separated by ampersand characters: http://www.irt.org/articles/js054/output.htm?myfield=abc123&otherfield=qwerty Receiving the form dataFor the receiving page to access this data, all that is required is to manipulate the search property of the current location:
The search property is first stripped of its initial question mark, and then held in passed. The getParm() function is used to return the value of the required parameter contained in passed. The values returned by getParm() are then used to update the formname form fields. Encoded CharactersThe original form may, however, have been completed with characters other than a-z and 0-9, in which case the browser will have encoded then. This is why we need to use the built in unescape() function to convert these encoded characters back to their original state. If the original form fields had been complete with spaces, then these will have been converted to plus signs. It is necessary to change these back to spaces before attempting to use unescape(). It is simple to perform this on the whole search string rather than the individual field values:
The replace() function simply replaces any occurences of text with by within string. Now when we strip the question mark from the beginning of the search property we can change all the plus signs to spaces:
Check Boxes
When the previous form is submitted the location of the next page will be: http://www.irt.org/articles/js054/output.htm?check1=X
Note that the second check box
Radio ButtonsConsider the following form:
When submitted the location of the next page will be: http://www.irt.org/articles/js054/output.htm?radio1=X Instead we require the values of the radio buttons to indicate their index with the current radio group (a radio group is a group of radio buttons all with the same name):
Which when submitted the location of the next page will be: http://www.irt.org/articles/js054/output.htm?radio1=0 We can then set the correct radio button on the next page using:
Select OptionsIf we treat these similar to radio buttons, where the value indicates the index of the option, then we can use the existing mechanism to pass the data to the next page:
Which when submitted the location of the next page will be: http://www.irt.org/articles/js054/output.htm?chosen=0 We can then set the correct radio button on the next page using:
Complicated FormsThe previous mechanism of passing data from one form to another will not work for forms that require drop down multiple select options.
Which when submitted the location of the next page will be: http://www.irt.org/articles/js054/output.htm?chosen=0&chosen=1 Only the first occurence of chosen will be found by the getParm() function. A different approach is need for forms containing drop down multiple select options. Rather than find the required parameter in the search property each time, which does incur an overhead, we can split the name-value pairs passed across into an array using the string method split(). As the split() method was introduced in JavaScript 1.1. we'll need to create a split mechanism for browsers that only support JavaScript 1.0.
Once we've replaced the plus signs with spaces in the search property, we additionally replace the equal signs with ampersands. This will enable us to split the name and values up into separate items, e.g. chosen=0&chosen=1 Will become: chosen&0&chosen&1 If the browser supports JavaScript 1.2 then the string split() method will be used otherwise the earlier define split() function will be used. Both will split the string using the text character passed, placing the contents in the splitArray[]. Therefore the splitArray[] would in this instance contain: chosen 0 chosen 1 As you can see the array will contain the names of the form field followed by its value. If the chosen form field were a text box, we would update it with:
Where indexEntry is the correct location within the splitArray[] for the value of chosen. Rather than search through the array for the required form fields and their values, it is better to process the array an entry at a time. For example the following form:
could be easily copied using the following script:
Note, that the radio buttons have a dummy onClick event handler, which is a workaround for older browsers that mix up the index order of the radio buttons. Note, also the need to subtract zero from the multiple select options index, this converts the string value to a numeric value, required again for older browsers. Working ExampleTry this example: Input Form. Source CodeYou can view the source code of the two pages: Feedback on 'Passing data from one form to another'
View the profile on Martin Webb and the list of other Articles by Martin Webb. |
-- div -->
|