PDA

View Full Version : Retrieving info from a url


ia021170
10-29-2003, 07:43 AM
Is there a way to pass information into a new page using the method=get in a form only using html and javascript? It's for a simple quiz with 1 question on each page:

<form id="Q1" action="2.htm" method="get">
<input type="radio" name="q1" onClick="0" value="1">
<input type="radio" name="q1" onClick="0" value="0">
<input type="radio" name="q1" onClick="0" value="0">
<input type="submit">
</form>

This submits the information into the URL on the next page but i don't know how to get the info from the URL. Is it possible??

jaeman
10-29-2003, 08:57 AM
Yes you can send the data to a javascript function on the same page but you wont be able to send it to a new document.

I was actually looking at what you are after the other day, i'll have a look for it, but i don't think you need method for this, just the action="javascript:funcNAME();"

<html>

<head>
<script type="text/javascript" language="javascript">
function showRESULTS() {
var dw=document.write; // may not work with NS7
dw(document.form1.q1.value);
}
</script>
</head>

<body>
<form name="form1" action="2.htm" method="get">
<input type="radio" name="q1" onClick="0" value="1">
<input type="radio" name="q1" onClick="0" value="2">
<input type="radio" name="q1" onClick="0" value="3">
<input type="submit">
</form>
</body>

</html>

Gotta go, i'll fix it soon... jaeman, something like that..!

COBOLdinosaur
10-29-2003, 12:24 PM
If you send a form to an HTML page instead of a processing script, the form values will be passed across as arguments. So you sent liske this:

<html>
<head>
</head>
<body>
<form name="form1" action="newpage.htm" method="get">
<input type="radio" name="q1" onClick="0" value="1">
<input type="radio" name="q1" onClick="0" value="2">
<input type="radio" name="q1" onClick="0" value="3">
<input type="submit">
</form>
</body>
</html>

Then on the page you are going to you extract the value this way:

<html>
<head>
<script>
<!--
parmarr = new Array;
function readparms()
{
if(location.search!='')
{
Args = location.search.substring(1);
parmarr = Args.split('=');
}
alert ('value is = '+parmarr[1]);
}
//-->
</script>
</head>
<body onLoad="readparms()">
The normal content of the page
<br />
A form element here can be populated instead of alerting the value
</body>
</html>

ia021170
10-29-2003, 02:15 PM
Got it working now but just one more thing, how do i pass on the same value along with another value to the next page??

Would i put it into a hidden element in a form like below? How do i do this??

<html>
<head>
<script>
<!--
parmarr = new Array;
function readparms()
{
if(location.search!='')
{
Args = location.search.substring(1);
parmarr = Args.split('=');
}
alert ('answer = '+parmarr[1]);
}
//-->
</script>
</head>
<body onLoad="readparms()">
<br />
<form id="Q2" action="question3.htm" method="get">
<input type="hidden" name="q1" value="">
<input type="radio" name="q2" onClick="0" value="1">
<input type="radio" name="q2" onClick="0" value="2">
<input type="radio" name="q2" onClick="0" value="3">
<input type="submit">
</form>
</body>
</html>

p.s. thanks for all the help so far!!!

n8thegreat
10-29-2003, 04:31 PM
this was a function i posted in another thread, it takes the values out of the query string, and creates javascript variables by the name of the thing in the string, and the value

function parseURL(){
URL = new String( document.location );
vars = URL.split("?");
if( vars.length > 1 ) {
vars = vars[1];
vars = vars.split("&");
for( var num = 0; num < vars.length; num++ ) {
vars[num] = vars[num].split("=");
}
for( var num = 0; num < vars.length; num++ ) {
eval( vars[num][0] +" = '"+ vars[num][1] +"'" );
}
}
}


so if your query string is ?bob=poo, it will create a variable by the name of bob with the value poo
and you can do as many variables in the query string as you want, seperate them with &
?bob=poo&foo=bar will create two variables, bob and foo

jaeman
10-30-2003, 03:25 AM
This is the end result of the form example posted earlier:

<html>
<head>
<script TYPE="text/javascript">
var ans = 0;
function showRESULTS() {
var ans = 0;
for(i=0;i<3;i++) {
if(eval("document.form1.q1["+i+"].checked")) {
ans=parseInt(eval("document.form1.q1["+i+"].value"));
}
}
document.write('Value = ',ans);
}
</script>
</head>

<body onload="document.form1.reset();">
<form name="form1">
<input type="radio" name="q1" value="1">
<input type="radio" name="q1" value="2">
<input type="radio" name="q1" value="3">
<input type="submit" value="Submit" onClick="showRESULTS();">
&nbsp
<input type="reset" value="Reset">
</form>
</body>

</html>

Thought i'd still post it, everyone else had there own so thats mine... jaeman

piglet
10-30-2003, 07:33 AM
Hi Folks,

You're missing a couple of important points:

1. Where the data is passed on the URL it's escape()d - so when you read it back you should always unescape it
2. Some browsers substitute "+" signs for spaces instead of %20 - you need to replace them before unescaping.

Here's a link (http://www.webxpertz.net/faqs/jsfaq/passvars.php#formurl) to a FAQ I wrote up which demonstrates a sript which (I think) covers the issues.

eval()? [shudder /]:O@