We help IT Professionals succeed at work.

Retrieve variables from Query String using asp.net

TechMonster
TechMonster used Ask the Experts™
on
I have a query string with first name and lastname.  
I was told to use the attached code to display the info..but it does not seem to be worklng.
my querystring variables looks like      ?fn=myFirstname&ln=myLastname

I would like to be able to pass these into the body...I have tried to do the Alert(lastname) but nothing comes up but "undefined"

Thanks.
<SCRIPT type ="text/javascript">
function qs() {
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
      
var firstname = qs['fn'];
var lastname = qs['ln'];
 
      
      
} 

</script>




</head>



<body>




</body>

</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
and are you calling the function qs somewhere?
Awarded 2011
Awarded 2011

Commented:
This does not belong to  Java Programmimg Language zone.

Commented:
also, qsParms is never defined, and in the function you are using qs['ln'] but qs is a function not an arrays...
try this instead:

<SCRIPT type ="text/javascript">
function qs() {
	var qsParm=new Array();
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
} 





var info=qs();

var firstname = info['fn'];
var lastname = info['ln'];

alert(firstname);
alert(lastname);


</script>

Open in new window

Author

Commented:
I thought I did put this is javascript..sorry.

Stalhw - The code works great...would you happen to know how to pass them into a textbox in html.

<%@ Page Language="C#" %>
<html dir="ltr" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">

<head runat="server">
<META name="WebPartPageExpansion" content="full">
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:PublishingContactPicture msdt:dt="string">, </mso:PublishingContactPicture><mso:PublishingContact msdt:dt="string"></mso:PublishingContact></mso:CustomDocumentProperties></xml><![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled 1</title>


   
 



</head>



<body>
<SCRIPT type ="text/javascript">
function qs() {
      var qsParm=new Array();
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
}





var info=qs();

var firstname = info['fn'];
var lastname = info['ln'];
//document.write(firstname + ' ' + lastname);
</script>

<label id="lblFirstName">First Name </label><textarea name="txtFirstName" >firstname</textarea>
<br>
<label id="lblLastName">Last Name </label><textarea name="txtLastName">lastname</textarea>

</body>
</html>
Commented:
Sure,
but first, why are you using textarea instead of <input type=text>
second, if this is a C# page, it would be much easier to get the LN and FN from C# than to do it via JavaScript...
And your page is missing a <form> </form> around the input...

but here:



<%@ Page Language="C#" %>
<html dir="ltr" xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">

<head runat="server">
<META name="WebPartPageExpansion" content="full">
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:PublishingContactPicture msdt:dt="string">, </mso:PublishingContactPicture><mso:PublishingContact msdt:dt="string"></mso:PublishingContact></mso:CustomDocumentProperties></xml><![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled 1</title>

<SCRIPT type ="text/javascript">
function qs() {
      var qsParm=new Array();
      var query = window.location.search.substring(1);
      var parms = query.split('&');
      for (var indx=0; indx<parms.length; indx++) {
            var pos = parms[indx].indexOf('=');
            if (pos > 0) {
                  var key = parms[indx].substring(0,pos);
                  var val = parms[indx].substring(pos+1);
                  qsParm[key] = val;
            }
      }
      return qsParm;
} 
</script>
</head>
<body>
<label id="lblFirstName">First Name </label><input type=text name="txtFirstName" id="txtFirstName" value="firstname">
<br>
<label id="lblLastName">Last Name </label><input type=text name="txtLastName" id="txtLastName" value="lastname">


<SCRIPT type ="text/javascript">
var info=qs();

var el;
el=document.getElementById('txtFirstName');
el.value=info['fn'];
el=document.getElementById('txtLastName');
el.value=info['ln'];
</script>

</body>
</html>

Open in new window