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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.


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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

717 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