Forum Moderators: open
I'm trying to combine two javascripts I've grabbed from the internet to provide the basic functionality of taking data from one .htm page and having it populate another .htm page.
The code example I've been using to try to test it is this:
First page (test.htm):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<form name="f" id="f" action="test2.htm" method="get">
<fieldset id="p_info" style="width:300px; padding:1em">
<legend>Personal information</legend>
Full Name:
<br />
<input type="text" name="fname" id="fname">
<br /><br />
Species:
<br />
<select name="species" id="species">
<optgroup label="Non-humanoid">
<option value="Changeling">Changeling</option>
<option value="Devidian">Devidian</option>
<option value="Species 8472">Species 8472</option>
<option value="Tholian">Tholian</option>
<option value="Xindi">Xindi</option>
<option value="other_non">Other Non-humanoid</option>
</optgroup>
<optgroup label="Humanoid">
<option value="Bajoran">Bajoran</option>
<option value="Betazoid">Betazoid</option>
<option value="Borg">Borg</option>
<option value="Cardassian">Cardassian</option>
<option value="Ferengi">Ferengi</option>
<option value="Human">Human</option>
<option value="Klingon">Klingon</option>
<option value="Romulan">Romulan</option>
<option value="Talaxian">Talaxian</option>
<option value="Tribble">Tribble</option>
<option value="Vulcan">Vulcan</option>
<option value="other_h">Other Humanoid</option>
</optgroup>
</select>
</fieldset>
<p>
<fieldset id="skills" style="width:300px; padding:1em">
<legend>Skills</legend>
<input type="checkbox" name="h_hardware" /> Holodeck - Hardware<br />
<input type="checkbox" name="h_software" /> Holodeck - Software<br />
<input type="checkbox" name="sec_ship" /> Security - Starship<br />
<input type="checkbox" name="sec_away" /> Security - Away Team Member<br />
<input type="checkbox" name="s_eng_rep" /> Starship - Engineering/Repair<br />
<input type="checkbox" name="s_admin" /> Starship - Administration<br />
</fieldset>
</p>
<input type="submit" value="Send Application" />
<input type="reset" value="Reset Form" />
</form>
</body>
</html>
------------
The page I'm using to try to grab it is this page (test2.htm):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<script type="text/javascript">
//<![CDATA[
// list of fields to get the data
function sendit (){
var fields = new Array('fname','species');
var querystring=''; // Go through a loop and add to the string IF a value was entered.
for (i=0;i<fields.length;i++) {
if (document.getElementById) {
if (document.getElementById(fields[i]).value != ''){
if (querystring != '')
{ querystring += '&'; }
querystring += document.getElementById(fields[i]).name + '=' +
document.getElementById(fields[i]).value; }
}
}
}
</script>
<body>
<form action="javascript:sendit();" name="f" id="f">
<p><label for="fromday">From Day:</label> <input type="text" name="fname" id="fname" value=""></p>
<p><label for="frommonth">From Month:</label> <input type="text" name="species" id="species" value=""></p>
</form>
</body>
</html>
------------------------
Problem: The query string shows the id/values in the url, and the form displays the fields to display, but they don't populate...I know it is probably a basic idea I'm missing here....any help?
Thanks!
// Fills a form with parameters passed by url.
// The standard format is //..../page.html?name1=value1&name2=value2
// Parameters that are not recognised are ignored.
// Parameter names must correspond with element names in the form.
// Names are NOT case sensitive.
// Values are not case sensitive but case is preserved for text fields.
// To set or clear a checkbox, use name=1 or name=0 respectively.
// To set a radiogroup value, use name=value where value is the value of the button to be checked.function loadParam(f,p) // f is a form, p is a string : 'name=value'
{ var pa = p.split('='); if (pa.length < 2) return false;var n = pa[0].toLowerCase();
var v = pa[1].toLowerCase();
with (f) {
for (var i = 0; i < length; i++) with (elements[i]) {
if (n == name.toLowerCase()) {
var tp = type.toLowerCase();
if (tp == 'select-one') {
for (var j = 0; j < length; j++) if (options[j].text.toLowerCase() == v) {
selectedIndex = j;
break;
}}
else if ((tp == 'radio') && (value.toLowerCase() == v)) checked = true
else if (tp == 'checkbox') checked = ((v != '') && (v.charAt(0) != '0'))
else value = pa[1]
}}
}}function loadParams(f) // f is a form
{ var tmp = unescape(document.location.search); if (tmp.charAt(0) == '?') tmp = tmp.substring(1);if ((typeof(f) == 'undefined') ¦¦ (tmp == '')) return false;
tmp = tmp.split('&'); for (var i = 0; i < tmp.length; i++) loadParam(f,tmp[i]);
}loadParams(document.forms[0]);
Kaled.