Javascript form processing

Posted on 2008-10-22
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" />

On the second page, the URL looks kind of like this if I entered 'xyz' into the input box:

How do I access whatever the user entered in the input box?
Question by:kgpretty
  • 3
  • 2

Expert Comment

ID: 22781067
Here's some example code I found on the web:

Kind regards,

Matthias Vance
LVL 82

Accepted Solution

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

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 "";
    return results[1];
var q = gup("query");
alert( q );

Open in new window


Author Closing Comment

ID: 31508953
Great answer! Works like a charm. Thankyou
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.


Author Comment

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 '+'?
LVL 82

Expert Comment

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

Expert Comment

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

return decodeURIComponent(results[1]);

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

773 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