How do I pass information to a page and do something with it.

  • Thread starter Thread starter Jane Here
  • Start date Start date
J

Jane Here

I want to go to a page on my web site and show two fields. I will use my own
program to open the web page. I want to pass to the web page the user's name
and their country, and have the web page display this information.

Example: Pass parameters to web site: Jane Here, New York.

Have the web page say: "Welcome Jane Here from New York."

Is the syntax to call the page something like this?

www.whatever.com/hello.com?name=JaneHere?city=New York

I don't know how to capture the variables passed to the page and then how to
use them to show the required text. Can someone get me started please?
 
Almost correct. When there is more than one name=value pair in the
QueryString the second and subsequent pairs are delineated with an ampersand

somewhere.com/example.asp?name=JaneHere&city=New%20York

Note the %20 hex value for a space in the URL. In the target page you can
get the values out of the QueryString using the Request object

Dim name
name = Request.QueryString("name")
Dim city
city = Request.QueryString("city")

Use the Response.Write code blocks to display the values inline...

Hello <%= name %> from <%= city %>.

Finally, you should use web search to learn more about passing values
between pages as there are other methods used for other reasons you'll want
to know how to use.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
 
Clinton,
Is your code ASP or what?

Jane Here,
I use this javascript code to extract the parameters:
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

In the HTML, add in the <head> section:
<script type="text/javascript">
var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>

The values of "name" and "city" will then be available in any JS used by
this page. It doesn't actually matter what you name the parameters when
passing them. The code in qsobj() doesn't care: the code in the <head>
section renames them.
 
Thanks for all the answers.

I made a simple html page to show the passed parameters. Can I request more
help to get it to work?
I am calling the page like this:
http://www.somewhere.com/getname.htm?name=JaneHere&city=New York

and want to say: Hello JaneHere from New York.

Here is the entire code. Can you see what needs correcting?

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
<script type="text/javascript">
var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>
</head>

<body>
Hello <%= name %> from <%= city %>.
<p><input type="text" name="name" size="20"></p>
<p><input type="text" name="city" size="20"></p>

<script type="text/javascript">
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

</script>

</body>
</html>





Trevor L. said:
Clinton,
Is your code ASP or what?

Jane Here,
I use this javascript code to extract the parameters:
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

In the HTML, add in the <head> section:
<script type="text/javascript">
var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>

The values of "name" and "city" will then be available in any JS used by
this page. It doesn't actually matter what you name the parameters when
passing them. The code in qsobj() doesn't care: the code in the <head>
section renames them.

--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
Almost correct. When there is more than one name=value pair in the
QueryString the second and subsequent pairs are delineated with an
ampersand
somewhere.com/example.asp?name=JaneHere&city=New%20York

Note the %20 hex value for a space in the URL. In the target page you
can get the values out of the QueryString using the Request object

Dim name
name = Request.QueryString("name")
Dim city
city = Request.QueryString("city")

Use the Response.Write code blocks to display the values inline...

Hello <%= name %> from <%= city %>.

Finally, you should use web search to learn more about passing values
between pages as there are other methods used for other reasons
you'll want to know how to use.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
 
Jane Here,
I would do it like this

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
<script type="text/javascript">
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>
</head>

<body>
<script type="text/javascript">
document.write("Hello " + name + " from " + city)
</script>
</body>
</html>

This will write to the page what is between the brackets in document.write

I saved it as test2.html and tested it with
test1.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
</head>
<body>
<a href = "test2.html?name=JaneHere&city=New%20York">Click here </a>
</body>
</html>

This worked fine. Of course you may want format it a bit pretttier.
 
Trevol L

Yes, that works! Excellent.

OK so now I want to go further and add some logic. Using a simple example,
can you show me how to do it?

Say I want to display the country based on the city.
Something like:

if city = "New%20York"
country = "USA"

if city = "London"
country = "UK"

if city = "Paris"
country = "France"


<body>
<script type="text/javascript">
document.write("Hello " + name + " from " + city + " country: " + country)
</script>
</body>
</html>

Jane


Trevor L. said:
Jane Here,
I would do it like this

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
<script type="text/javascript">
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>
</head>

<body>
<script type="text/javascript">
document.write("Hello " + name + " from " + city)
</script>
</body>
</html>

This will write to the page what is between the brackets in document.write

I saved it as test2.html and tested it with
test1.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
</head>
<body>
<a href = "test2.html?name=JaneHere&city=New%20York">Click here </a>
</body>
</html>

This worked fine. Of course you may want format it a bit pretttier.
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au

Jane said:
Thanks for all the answers.

I made a simple html page to show the passed parameters. Can I
request more help to get it to work?
I am calling the page like this:
http://www.somewhere.com/getname.htm?name=JaneHere&city=New York

and want to say: Hello JaneHere from New York.

Here is the entire code. Can you see what needs correcting?
 
OK Jane,
We're having fun here

You will need some more Javascript.

Your code is nearly correct.
You could code a function
function countryname(city)
{ if city == "New York" country = "USA"
if city == "London" country = "UK"
if city == "Paris" country = "France"
return country}

The only thing a little inefficient about this is that it keeps testing the
value of city even after a match has been found.
To avoid this, you can use "else"
function countryname(city)
{ if city == "New York" country = "USA"
else if city == "London" country = "UK"
else if city == "Paris" country = "France"
.............
else country = "Unknown"
return country}

Another way is the switch statement
function countryname(city)
switch (city)
{
case "New York" : country = "USA"; break
case "London" : country = "UK"; break
case "Paris": country = "France";break
....
default : country = "Unknown"
}
return country}

I like the other way of expressing the if else
variable = (condition1) ? 'value1' : 'value2'
In English this reads: If condition1 is true, then set variable to value1,
else set variable to value2.
The good thing about this is that instead of 'value2', you can add some more
conditions and values. The final value is the default value taken when all
others have been tested
e.g.
variable = (condition1) ? 'value1'
: (condition2) ? 'value2'
: (condition3) ? 'value3'
: 'defaultvalue"

So try this
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
<script type="text/javascript">
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

function countryname(cityname)
{var country = (cityname = 'New York') ? 'USA'
: (cityname = 'New York') ? 'UK'
: (cityname = 'Paris') ? 'France'
: 'Unknown'
return country}

var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
var country = countryname(city) </script>
</head>
<body>
<script type="text/javascript">
document.write("Hello " + name + " from " + city + " country: " + country)
</script>
</body>
</html>

The result of this using the same test1.html was:
Hello JaneHere from New York country: USA

Of course, another embellishment is ask for the name and city in test1.html
and then pass the answers as parameters to test2.html.
That's another story. If you want to do it, I (or many others) can give you
the code.
 
Jane
A small error in cutting and pasting the code

The function should be
function countryname(cityname)
{var country = (cityname = 'New York') ? 'USA'
: (cityname = 'London') ? 'UK'
: (cityname = 'Paris') ? 'France'
: 'Unknown'
return country}

Of course, New York still worked, but London would not have ;-(
 
Some user may have problem with spaces in the querystring, i.e., New York.

--
==============================================
Thomas A. Rowe (Microsoft MVP - FrontPage)
==============================================
If you feel your current issue is a results of installing
a Service Pack or security update, please contact
Microsoft Product Support Services:
http://support.microsoft.com
If the problem can be shown to have been caused by a
security update, then there is usually no charge for the call.
==============================================

Jane Here said:
Thanks for all the answers.

I made a simple html page to show the passed parameters. Can I request more help to get it to
work?
I am calling the page like this:
http://www.somewhere.com/getname.htm?name=JaneHere&city=New York

and want to say: Hello JaneHere from New York.

Here is the entire code. Can you see what needs correcting?

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
<script type="text/javascript">
var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>
</head>

<body>
Hello <%= name %> from <%= city %>.
<p><input type="text" name="name" size="20"></p>
<p><input type="text" name="city" size="20"></p>

<script type="text/javascript">
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

</script>

</body>
</html>





Trevor L. said:
Clinton,
Is your code ASP or what?

Jane Here,
I use this javascript code to extract the parameters:
function qsobj(parm)
{var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null}

In the HTML, add in the <head> section:
<script type="text/javascript">
var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
// add similar lines to extract extra parameters
</script>

The values of "name" and "city" will then be available in any JS used by this page. It doesn't
actually matter what you name the parameters when passing them. The code in qsobj() doesn't care:
the code in the <head> section renames them.

--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au
Almost correct. When there is more than one name=value pair in the
QueryString the second and subsequent pairs are delineated with an
ampersand
somewhere.com/example.asp?name=JaneHere&city=New%20York

Note the %20 hex value for a space in the URL. In the target page you
can get the values out of the QueryString using the Request object

Dim name
name = Request.QueryString("name")
Dim city
city = Request.QueryString("city")

Use the Response.Write code blocks to display the values inline...

Hello <%= name %> from <%= city %>.

Finally, you should use web search to learn more about passing values
between pages as there are other methods used for other reasons
you'll want to know how to use.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/


I want to go to a page on my web site and show two fields. I will
use my own program to open the web page. I want to pass to the web
page the user's name and their country, and have the web page
display this information. Example: Pass parameters to web site: Jane Here, New York.

Have the web page say: "Welcome Jane Here from New York."

Is the syntax to call the page something like this?

www.whatever.com/hello.com?name=JaneHere?city=New York

I don't know how to capture the variables passed to the page and
then how to use them to show the required text. Can someone get me
started please?
 
Jane,

My humblest apologies.

I fell into the trap that many do when starting to use Javascript adn
similar languages.
The trap is:
THE TEST FOR EQUALITY IS == , NOT = .
Your code is nearly correct.
You could code a function
function countryname(city)
{ if city == "New York" country = "USA"
if city == "London" country = "UK"
if city == "Paris" country = "France"
return country}
(Note the double eqauls signs.)
And yet when I coded the "smart" if then else code, I used a single =

The code should be (simplified a bit)
test1.html
======
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
</head>
<body>
<a href = "test2.html?name=JaneHere&city=London">Click here </a>
</body>
</html>
test2.html
======
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Write user name city and country</title>
<script type="text/javascript">
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null
}
function countryname(city)
{
return (city == 'New York') ? 'USA'
: (city == 'London') ? 'UK'
: (city == 'Paris') ? 'France'
: 'Unknown'
}
var name = qsobj(0)
var city = qsobj(1)
var country = countryname(city)
</script >
</head>
<body>
<script type="text/javascript">
document .write("Hello " + name + " from " + city + " country: " +
country)
</script>
</body>
</html>

It is a pity about the time difference. It is probably about midnight where
you are, so I guess you won't read this for a while.

You have taught me a lesson, though.
This next sentence is for me : "Don't send information.suggestions, etc.
until it is completely tested."
 
Hi Trevor

It is now working - you have taught me a lot.

In your function:-

function countryname(city)
{
return (city == 'New York') ? 'USA'
: (city == 'London') ? 'UK'
: (city == 'Paris') ? 'France'
: 'Unknown'
}

................................
Is the ":" exactly equivalent to "else if". In other words, is the function
above exactly the same as the one below?
................................


function countryname(city)
{
if (city == "New York") country = "USA"
else if (city == "London") country = "UK"
else if (city == "Paris") country = "France"
else country = "Unknown"
return country
}

Also, I tried to get your "case" syntax to work, but that doesn't display
anything.
Here it is................


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>

<script type="text/javascript">

function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null
}

function countryname(city)
switch (city)
{
case "New York" : country = "USA"; break
case "London" : country = "UK"; break
case "Paris": country = "France";break
default : country = "Unknown"
return country
}

var name = qsobj(0) //extracts first parameter
var city = qsobj(1) //extracts second parameter
var country = countryname(city)
</script>

</head>
<body>
<script type="text/javascript">
document.write("Hello " + name + " from " + city + " country: " + country)
</script>
</body>
</html>

............................................................
Thanks again
Jane.




Trevor L. said:
Jane,

My humblest apologies.

I fell into the trap that many do when starting to use Javascript adn
similar languages.
The trap is:
THE TEST FOR EQUALITY IS == , NOT = .
Your code is nearly correct.
You could code a function
function countryname(city)
{ if city == "New York" country = "USA"
if city == "London" country = "UK"
if city == "Paris" country = "France"
return country}
(Note the double eqauls signs.)
And yet when I coded the "smart" if then else code, I used a single =

The code should be (simplified a bit)
test1.html
======
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Get user name and city</title>
</head>
<body>
<a href = "test2.html?name=JaneHere&city=London">Click here </a>
</body>
</html>
test2.html
======
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Write user name city and country</title>
<script type="text/javascript">
function qsobj(parm)
{
var qpairs = document.location.search.substring(1).split("&")
var qvbl = qpairs[parm].split("=")
return qvbl[1] ? unescape(qvbl[1].replace(/%20|\+/g," ")) : null
}
function countryname(city)
{
return (city == 'New York') ? 'USA'
: (city == 'London') ? 'UK'
: (city == 'Paris') ? 'France'
: 'Unknown'
}
var name = qsobj(0)
var city = qsobj(1)
var country = countryname(city)
</script >
</head>
<body>
<script type="text/javascript">
document .write("Hello " + name + " from " + city + " country: " +
country)
</script>
</body>
</html>

It is a pity about the time difference. It is probably about midnight
where you are, so I guess you won't read this for a while.

You have taught me a lesson, though.
This next sentence is for me : "Don't send information.suggestions, etc.
until it is completely tested."
--
Cheers,
Trevor L.
Website: http://tandcl.homemail.com.au

Jane said:
Hi Trevor

Thanks.
Whatever info I enter, it gives me country: USA
Hello JaneHere from London country: USA

It is here:
http://www.usbdial.com/get2.htm?name=JaneHere&city=London

I also tried the elseif function but that didn't return anything:
http://www.usbdial.com/get1.htm?name=JaneHere&city=London

I then tried the switch function but that didn't return anything:
http://www.usbdial.com/get3.htm?name=JaneHere&city=London

Can you see what I am doing wrong?

Jane
 
Jane,

Some quick answers before I have dinner (here in Canberra, Australia)

Yes, the ":" is equivalent to "else if"

Re the case statement, there may be some extra needed. I have only used it
once. Perhaps the conditions need to be in brackets.
E.G.
function countryname(city)
switch (city)
{
case ("New York") : country = "USA"; break
case ("London") : country = "UK"; break
case ("Paris"): country = "France";break
default : country = "Unknown"
return country
}

It is also possible the assignment needs to be in {}
E.G.
function countryname(city)
switch (city)
{
case ("New York") : {country = "USA"}; break
case ("London") : {country = "UK"}; break
case ("Paris"): {country = "France"};break
default : {country = "Unknown"}
return country
}

You could try either or both of these. I may do so later
 
Jane,
After dinner.

A bracket "}" was in the wrong place. There must be one to close the switch
statement.
The next statement "return country" must be outside the switch and before
the "}" which closes the function

This is the corrected function
function countryname(city)
{
switch (city)
{
case "New York" : country = "USA"; break
case "London" : country = "UK"; break
case "Paris" : country = "France"; break
default : country = "Unknown"
}
return country
}
In the switch statement, the : is a separator that says "if the value before
the : is true, then the statement(s) after the : is(are) executed"

In the special "if then else" statement in the following function, the ?
has the meaning "if the statement before the ? is true, then the value
after the ? is used"; the : says "else if"
function countrynamex(city)
{return (city == 'New York') ? 'USA'
: (city == 'London') ? 'UK'
: (city == 'Paris') ? 'France'
: 'Unknown'
}
This is/can be confusing

Sorry about my mistake (yet again). I make far too many slip-ups like this.
 
Hi again Trevor

All working.

Thank you very much for going out of your way to explain things.
This has been a good introduction to the wonderful world of JavaScript!

Many thanks
Jane
 
Back
Top