Click to See Complete Forum and Search --> : Long Query String Question


Force
08-11-2003, 03:49 PM
I have a form that I'm pulling out the values with Javascript so that a person can review the form in a pop up window. I'm using a Javascript function under an OnClick method. Rather than hand code the Query string (There are 150 fields), is there some way to "Scrape" the form with javascript so that the Query String can be generated dynamically? Maybe some kind of Javascript loop that searches the DOM for objects? I'm fairly new to Javascript so I appreciate the help.
Thanks.

Mark F

SlankenOgen
08-11-2003, 04:00 PM
It really depends on what kind of fields they are, but say they are text fields in one form, you can use-

var lgn = document.Formname.length;

for(j = 0; j < lgn; j++){
if(document.Formname[j].type == "text"){

myVar += document.Formname[j].value;

}

On the other hand you can give the fields serial names like

a1, a2, etc., then use-

myVar += document.Formname["a"+j].value;

pyro
08-11-2003, 04:40 PM
You could use something like this, which will print it out in name/value pairs:

<script type="text/javascript">
query = window.location.search.substr(1);
if (query.length > 0) {
newwin = window.open();
vals = query.split(/[&;]/);
for (i=0; i<vals.length; i++) {
name_val = vals[i].split(/=/);
newwin.document.write("<b>"+name_val[0]+":</b> "+name_val[1]+"<br>");
}
}
</script>

Force
08-12-2003, 09:25 AM
Pyro,

I see what you are trying to do, but when I create this code as a function, nothing happens when I use the onClick event.
The new window doesn't generate.

As I see it the line:
query = window.location.search.substr(1); searches for formfields. Does it just take formfield (1) or will I have to loop it over the number of formfields?

Thanks for the help in this.

Mark F

pyro
08-12-2003, 09:34 AM
No, the code I posted will loop through all the fields. You need to make sure you are using the GET method with the form. Try this code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
query = window.location.search.substr(1);
if (query.length > 0) {
newwin = window.open();
vals = query.split(/[&;]/);
for (i=0; i<vals.length; i++) {
name_val = vals[i].split(/=/);
newwin.document.write("<b>"+name_val[0]+":</b> "+name_val[1]+"<br>");
}
}
</script>
</head>
<body>
<form action="testingform.htm" method="get">
<p><input type="text" name="field1"><br>
<input type="text" name="filed2"><br>
<input type="submit"></p>
</form>
</body>
</html>

Force
08-12-2003, 01:02 PM
Pyro,

That's one sweet piece of code. Nice!.
I just need to figure out how to mod it a little. The form that I'm scanning already has an action page for submission. I want to be able to send these results to another template for a visual review.
Again, thanks very much for the help. Great work.

Mark F

pyro
08-12-2003, 01:21 PM
You are very welcome... :)