Using Parameters To Update Form

[Total: 0    Average: 0/5]

This example looks at the arguements (via a GET method URL re-write).

Body Code:

<form name=exf1>
<input type=text name=param1 value="default1"><BR>
<input type=text name=param2 value="default2"><BR>
<input type=text name=param4 value="default4"><BR>
</form>

<script>
var _l = document.location.toString();
    // This next line is here for this test for show only.
	// normally you would remove it.
    _l = "http://www.js-examples.com/?param1=newval1&param2=secondval&param3=thirdone&param4=numberfour";
// Parameter/Value pairs follow a "?" and then are delinated by "&"
// they have a "=" between them.

// for this example - we assume that the parameter name exists in the form.
var PS = new Array();
var _i = _l.indexOf("?");
if (_i != -1) {
  // then there is a "?" in the url
  _l = _l.substr(_i+1,_l.length);  // lets only deal with the parameters
  //document.write("before loop logic: _l="+_l+"<BR>");
  while (_l.indexOf("&") != -1) {
    // pull each param/value pair out until there is only 1 left.
	var _a = _l.substr(0,_l.indexOf("&"));
	//document.write("#1:"+_a+"<BR>");
	PS[PS.length?PS.length:0] = _a;
	var _m = _l.substr(1,_l.length).indexOf("&");
	    _m++; // move 1 past the &
	//document.write("_m="+_m+"<BR>");
	if (_m == 0) _m = _l.length; // if there are no more & - get the rest of the url string.
	_l = _l.substr(_m+1,_l.length);
	//document.write("_l = "+_l+"<BR>");
  }
  // pull off the last param/value pair.
  //document.write("#2:"+_l+"<BR>");
  PS[PS.length?PS.length:0] = _l;
}
//document.write("DONE<BR>");
for (var _i=0;_i<PS.length;_i++) {
  var _a = PS[_i].indexOf("=");
  var _b = PS[_i].substr(0,_a);
  var _c = PS[_i].substr(_a+1,PS[_i].length);
  //document.write("P/V #"+_i+": "+PS[_i]+"==> "+_b+"="+_c+"<BR>");
  if (document.exf1)
  if (eval("document.exf1."+_b))
  eval("document.exf1."+_b+".value='"+_c+"'");
}
</script>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *