Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Picking up form values with javascript 2

Status
Not open for further replies.

link9

Programmer
Nov 28, 2000
3,387
US
Hello all --

I've always used the request.form vbscript method to pick up form values from previous pages, but was confronted with a situation yesterday where I really needed to know how to do this with javascript.

Can anyone tell me how to do that?

Thanks for the input!:)
Paul Prewett
 
if you want to pick up those values for validation purpose, do it on page where the form is
---
<head>
<script language=javascript>
function validate() {
if (document.myform.mytext.value==&quot;whatever&quot;) ...
.....
</script>
</head>
<body>
<form name=myform.... onsubmit=&quot;validate()&quot;>
<input type=text name=mytext ....>
....
----
this was ie4+ only !! (maybe ns4+ as well .....)
----
 
No, that's not what I want.

To clarify what I meant, I would like to find out what the values of the form elements were on the previous page after the user clicked 'Submit'...

To pick up the value of an element called 'myElement' on the subsequent page in VBScript, I say:
Code:
request.form(&quot;myElement&quot;)
and then place that into a variable...

Is there a way to do the same thing, only with client side javascript?

Thanks
Paul Prewett
 
there's only one way to do that !!
you must use a form with a GET method and use a script like this one :

ejs_list = new Array;
ejs_list_valeur = new Array;
ejs_place = document.location.href.indexOf(&quot;?&quot;,0);
if(ejs_place >= 0)
{
ejs_query_string = document.location.href.substring(ejs_place+1, document.location.href.length);
ejs_place_and = ejs_query_string.indexOf(&quot;&&quot;,0);
if(ejs_place_and >= 0)
ejs_list = ejs_query_string.split(&quot;&&quot;);
else
ejs_list[0] = ejs_query_string;
for (ejs_i=0;ejs_i<ejs_list.length;ejs_i++)
{
ejs_temp = ejs_list[ejs_i].split(&quot;=&quot;);
ejs_variable = ejs_temp[0];
ejs_valeur = ejs_temp[1];
ejs_plus_place = ejs_valeur.indexOf(&quot;+&quot;,0);
while (ejs_plus_place >-1)
{
ejs_temp2 = ejs_valeur.substring(0,ejs_plus_place) + ' ' + ejs_valeur.substring(ejs_plus_place+1,ejs_valeur.length);
ejs_valeur = ejs_temp2;
ejs_plus_place = ejs_valeur.indexOf(&quot;+&quot;,ejs_plus_place+2);
}
ejs_list_valeur[ejs_variable] = ejs_valeur;
}
}

function get(ejs_get)
{
if(ejs_list_valeur[ejs_get])
return(unescape(ejs_list_valeur[ejs_get]));
else
return(&quot;&quot;);
}


This script allows you to take element from the QueryString so you just have to make a function on your submit button who's gonna construct your URL like for a Request.QueryString:
Let me know if it solve your probleme
 
I knew I started using VBScript for some reason...

Thank you, SeAl, for your response. It is completely clear to me that the 'hack' that I used yesterday is exactly what I'm going to continue to use.

If anyone is interested, here goes...

This is asp server side script that actually writes out my javascript function dynamically depending on however many different variables it needs to depend on (I'll use one here just for brevity's sake):
Code:
<%
dim myValue
myValue = request.form(&quot;myElement&quot;)

with response
  .write(&quot;<script language=javascript>&quot; & vbcr)
  .write(&quot;function myFunction(){&quot; & vbcr)
  .write(&quot;document.myForm.myElement.value == &quot; & myValue & &quot;;&quot; & vbcr)
  .write(&quot;}&quot; & vbcr)
  .write(&quot;</script>&quot;)
end with
%>

The resulting code comes out looking just like any other javascript function (with whatever value is in the form variable) and looks to me MUCH easier that what was posted above (although I'm admitting it's a hack)...

I guess you could use this technique to write out just as complicated a javascript function as you wanted. The one above is obviously pretty simple, and only serves to illustrate the point.

Hey... whatever works, right? ;-)

Thanks --
Paul Prewett
 
just wondering, why do you bother passing those variables in js when you can use asp ?? i mean, i'm curious *why* you would pass values to the next page ???
 
Well, I use javascript for some things (form validation, changing the values of form elements, etc...), and I use asp (vbScript) for everything else including my database access and such.

I was trying to implement a skip pattern for an online survey I was developing, and the problem presented itself that it would have been more efficient for me to just pick up the value in javascript to change the value of a hidden form element so that I would know whether or not to implement the skip pattern.

As it turns out, everything works fine with the hack that I posted above. I was just curious to know if there was an easy way to do it in javascript so that I wouldn't have to use it.

-----
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top