Setting query string value to a text box

Hi,
Im new to javascript and have a question on syntax to basically do this:

on page load, check if there is a query string called frmdate.

If there is, I set the text box on the page called “myDate” equal to the query string variable.

full syntax is much appreciated, thanks much.

-JBD

easiest way would be a function called from the onload event of the body tag:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> new document </title>
<script type='text/javascript'>
<!--
  function setField()  {
  var qString = location.search;
  if (qString.indexOf('frmdate') != -1) {
     var data = qString.substring(qString.indexOf('=') + 1, qString.length);
     document.forms[0].myDate.value = data;
    }
  }
//-->
</script>

</head>

<body onload='setField()'>

<form method=post action="">
<input type="text" name="myDate">
</form>
</body>
</html>

Now this will only work if that frmdate is the ONLY variable coming in because of the substring being used on the “=” (IE frmdate=data) … if there is more than one variable you would have to get the position of frmdate and then move it over to get the value after it…

You can also change the document.forms[0] to the form name if you have it named … IE your form has name=‘myForm’ in the form tag you would then change the js to document.myForm.myDate.value =

Hi thanks.

I do on occasion have different variables in the string. Mostly my querystring looks like this:

?city=Vail&frmDate=01/04/2005

but sometimes it looks like this:

?city=Aspen&frmDate=01/19/2005?sc=yes

the position of it is still the same…so how would I move it over? the city variable length changes. Thanks.

OK then you will need to find the indexOF the variable frmdate and the use it to “move over” to its value …

Ill drop some more code after I eat my lunch here (its getting cold) :frowning:

OK heres a new version that first finds the variable in the query string and then grabs its value…

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> new document </title>
<script type='text/javascript'>
<!--
  function setField()  {
  var qString = location.search;	// get the query string

  if (qString.indexOf('frmDate') != -1) {	// find out if the field frmDate is present
	var requiredData = qString.indexOf('frmDate=');	// get the postion in the query string of frmDate
	var data = qString.substring(requiredData + 8, qString.length);		// grab everything in the query string AFTER frmDate
	
	if (data.indexOf('&') != -1)	{		// check to make sure there are no other variables AFTER the frmDate variable
		data = data.substring(0, data.indexOf('&'));		// if there is more data after it chop it off
		}
	
     document.forms[0].myDate.value = data;		// assign the proper data to the form field
    }
  }
//-->
</script>

</head>

<body onload='setField()'>

<form method=post action="">
<input type="text" name="myDate">
</form>
</body>
</html>

Ive broken all the variable and substrings in to their own steps to help you understand it even though the code COULD be condensed quite a bit… didnt want to confuse you! :slight_smile:

OH and BTW, you CANT be joeybagodonutz … cause he’s OVA HEYA in NY! :smiley:

Something that may be of use when chopping up query strings is split():

var qstring = location.search;
var arrayA = qstring.split(“?”);

(How do you put 2 questions marks in a query string?)

Then, on each string in arrayA, you can use split(“&”).
And, on each of those strings, you can use split(“=”);

Yeah split() is fine for more advanced users but this person seems (i think even said) they are a beginner so I figured the KISS method was the best :agree:

Joeybagadonutz give this a try


<form><input></form>
<script>
onload = function() {
	var m = location.search.match(/[?&]frmdate=([^&]+)/i);
	if(m)
		document.forms[0][0].value=unescape(m[1]);
}
</script>

Let me know if you need any explanations.