Solved

Javascript form processing

Posted on 2008-10-22
6
325 Views
Last Modified: 2012-06-27
When I submit a form using the get method and it takes to the next html page. how do I access values from the previous page ?


Here's my form from the first page:

<form action="searchResults.html" method="get">
Search for the database:
<input type="text" name="query" />
<input type="submit" alue="Go" />
</form>

On the second page, the URL looks kind of like this if I entered 'xyz' into the input box:
file:///E:/Project/searchResults.html?...

How do I access whatever the user entered in the input box?
0
Comment
Question by:kgpretty
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22781067
Here's some example code I found on the web:
http://www.idealog.us/2006/06/javascript_to_p.html

Kind regards,

Matthias Vance
0
 
LVL 82

Accepted Solution

by:
hielo earned 50 total points
ID: 22781080
in searchResults.html use:

<script>
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
var q = gup("query");
alert( q );
</script>

Open in new window

0
 

Author Closing Comment

by:kgpretty
ID: 31508953
Great answer! Works like a charm. Thankyou
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kgpretty
ID: 22781162
Oops.. I think I accepted to early. What if the query submitted through the form had spaces in? That would mean the text comes up on my searchResults page with '+' where the spaces were. How do I remove the '+'?
0
 
LVL 82

Expert Comment

by:hielo
ID: 22782026
var q = gup("query").replace(/[+]/g,' ');
0
 
LVL 82

Expert Comment

by:hielo
ID: 22786100
An alternative to the replace() I posted earlier would be to change:
return results[1];


to:
return decodeURIComponent(results[1]);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now