# javascript pass variable next page



## mohityadavx (Feb 9, 2012)

Well i am having a problem.

i have made a form and on clicking the submit button,  javascript validation is done to check all fields are correctly filled or not. Now the problem is I want to use these entered variables so the only way remains is redirect the page to next page after clicking submit using "action = page.html" tag.

So how can i pass these variable to next page. I cant use PHP but javascript (Its kind of a constraint on me  ) 

Well what i want is this site Kagzaat - Free Legal Online Documentation( www.kagzaat.com) to be in javascript instead of PHP . Please help.


if any other way exist please tell.


----------



## Abhinav1217 (Feb 9, 2012)

Try* Javascript Sessions* or *javascript cookies*..
this might get you started
Cookie-less Session Variables in JavaScript » SitePoint
How to save session values in JavaScript - JavaScript / DHTML / AJAX | DaniWeb


----------



## RCuber (Feb 9, 2012)

you can use two kinds of approach. 
*1. use global JS variables, include the variables in a JS file and refer those files in both the pages. *
Ex: create test.js 


```
// filename.js
var test1;
var test2;
```

then refer this JS file in both the pages. 

in the first page, set the variables after your validation. 

in the second page retrieve the values from the variable. 

*2. use Query string(this is better and easy). *

query strings are the name value pairs which are passed in the url itself. 

eg: _http://test.com/submitpage.html?key1=value1&key2=value2 

here is a sample for accessing query strings using Javascripts. 


```
var qsParm = new Array();
function qs() {
var query = window.location.search.substring(1);
var parms = query.split('&');
for (var i=0; i<parms.length; i++) {
var pos = parms[i].indexOf('=');
if (pos > 0) {
var key = parms[i].substring(0,pos);
var val = parms[i].substring(pos+1);
qsParm[key] = val;
}
}
}
```

Source for #2


----------



## mohityadavx (Feb 9, 2012)

Thanx


----------



## Zangetsu (Feb 10, 2012)

I recommend using query strings


----------



## tonydisalva (Jan 16, 2013)

Hello

   I have recommend using query strings  of the java in JS file is very important technique.


----------



## abhidev (Jan 16, 2013)

RCuber said:


> you can use two kinds of approach.
> *1. use global JS variables, include the variables in a JS file and refer those files in both the pages. *
> Ex: create test.js
> 
> ...


How are u gonna retain the global variables on different pages when the pages are itself getting refreshed :/ ...unless he is using ajax.


----------



## aman@odigma.com (Jan 17, 2013)

you can use query string or you can use ajax


----------

