I want to pass my value of one variable to another html page using query string.
first.html:
EDIT: I've edited slightly and this seems to work for me, what exactly is the problem?
Please explain what "required result" is..
Page 1
<form id="form1" name="form1" method="get" action="2.html">
<input type="text" name="txtFileName" id="txtFileName"/>
<input type="hidden" name="hid1" value="">
<br><input type="submit" value="Send me your name!" onClick="submitform();">
<br>
</form>
<script type="text/javascript">
function submitform()
{
document.form1.hid1.value="hidden value";
document.form1.submit();
}
</script>
Page 2
<SCRIPT LANGUAGE="javascript">
function getQueryVariable2(variable) {
var query = window.location.search.substring(1);
document.write(query);
var vars = query.split("&");
document.write("<br />");
document.write(vars);
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
document.write("<br />txtFileName = " + getQueryVariable2("txtFileName"));
document.write("<br />hid1 = " + getQueryVariable2("hid1"));
</SCRIPT>
Have a look at this link http://www.htmlgoodies.com/beyond/javascript/article.php/3471111/A-Quick-Tutorial-on-JavaScript-Variable-Passing.htm
Add method="get"
to the form
element. Otherwise, POST
(the default) might be used which passes parameters in a different way.
See: FORM method attribute