Avatar of TechMonster
TechMonster
 asked on

Retrieve variables from Query String using asp.net

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

JavaJavaScript

Avatar of undefined
Last Comment
stalhw

8/22/2022 - Mon
stalhw

and are you calling the function qs somewhere?
for_yan

This does not belong to  Java Programmimg Language zone.
stalhw

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
TechMonster

ASKER
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>
ASKER CERTIFIED SOLUTION
stalhw

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question