how to pass two cookies together to another html-page?

Forums HTMLhow to pass two cookies together to another html-page?
Staff asked 2 years ago

Hi

I’m trying to pass firstname and name from a html page to another html page (without using server code like php or asp.net) using cookies. This works partially because i get the firstname in the first input of the page action.html but also in the second input. How can i get the name in the second input in action.html?
Thanks
V.

first page:
<form action=”action.html”>
<label>Firstname:</label><br>
<input type=”text” id=”vn” autofocus ><br>
<label>Name:</label><br>
<input type=”text” id=”an”><br><br>
<input type=”submit” onclick=”MakeCookie()”>
</form>
<script type=”text/javascript”>
function MakeCookie()
{
var vn = document.getElementById(“vn”).value;
var an = document.getElementById(“an”).value;
document.cookie = an;vn;
}
</script>
———————–
action.html:

<input id=”vn” type=”text” />
<input id=”an” type=”text” />
<script type=”text/javascript”>
cc = document.cookie.split(“;”);
document.getElementById(“vn”).value = cc;
document.getElementById(“an”).value = cc;
</script>

Instead of cookies, you can use query string to another HTML page in the following way:

1.html

<html>
<head>
<title>Page Title</title>
</head>
<body>

<form action="f2.html?a=aaa">
  <label>Firstname:</label><br>
  <input type="text" name="vn" autofocus ><br>
  <label>Name:</label><br>
  <input type="text" name="an"><br><br>
  <input type="submit" value="Submit">
</form>
</html>

2.html

<html>
<head>
<title>Page Title</title>
</head>
<body>

</body>

<script>
    var url = window.location.href;
  var vars = {};
  var hashes = url.split("?")[1];
  var hash = hashes.split('&');

  for (var i = 0; i < hash.length; i++) {
    params=hash[i].split("=");
    alert(params[1]);
  }
</script>
</html>

 

Prince Dhameliya replied 2 years ago

Answers (1)

Add Answer
Prince Dhameliya Marked As Accepted
Staff answered 2 years ago

Instead of cookies, you can use query string to another HTML page in the following way:

1.html

<html>
<head>
<title>Page Title</title>
</head>
<body>
<form action="f2.html?a=aaa">
  <label>Firstname:</label><br>
  <input type="text" name="vn" autofocus ><br>
  <label>Name:</label><br>
  <input type="text" name="an"><br><br>
  <input type="submit" value="Submit">
</form>
</html>

2.html

<html>
<head>
<title>Page Title</title>
</head>
<body>
</body>
<script>
    var url = window.location.href;
  var vars = {};
  var hashes = url.split("?")[1];
  var hash = hashes.split('&');
  for (var i = 0; i < hash.length; i++) {
    params=hash[i].split("=");
    alert(params[1]);
  }
</script>
</html>

 

 

Subscribe

Select Categories