Ask Your Question

Revision history [back]

The process of obtaining ItemCode, ItemName, ItemPrice and Qty from the user, saving them as cookies, and subsequently presenting them on the following webpage can be done as follows:

  1. Create a form on the webpage where the user can input the required information - ItemCode, ItemName, ItemPrice and Qty.

  2. When the user submits the form, use JavaScript to store the values as cookies. This can be done using the document.cookie property.

  3. To retrieve the values from the cookies on the following webpage, use JavaScript to read the values from the cookies using the document.cookie property.

  4. Use JavaScript again to display the values on the following webpage. This can be done using the innerHTML property to modify the HTML content of the page.

Example Code:

HTML for the form on the first page:

<form id="itemForm">
  ItemCode: <input type="text" name="itemCode"><br>
  ItemName: <input type="text" name="itemName"><br>
  ItemPrice: <input type="text" name="itemPrice"><br>
  Qty: <input type="number" name="qty"><br>
  <input type="submit" value="Submit">
</form>

JavaScript code to store the form data as cookies:

document.getElementById("itemForm").addEventListener("submit", function(event) {
  event.preventDefault(); // prevent the form from submitting

  // get the form data
  var itemCode = document.getElementsByName("itemCode")[0].value;
  var itemName = document.getElementsByName("itemName")[0].value;
  var itemPrice = document.getElementsByName("itemPrice")[0].value;
  var qty = document.getElementsByName("qty")[0].value;

  // set the cookies
  document.cookie = "itemCode=" + itemCode;
  document.cookie = "itemName=" + itemName;
  document.cookie = "itemPrice=" + itemPrice;
  document.cookie = "qty=" + qty;
});

JavaScript code to display the cookie values on the following webpage:

document.getElementById("itemCode").innerHTML = getCookie("itemCode");
document.getElementById("itemName").innerHTML = getCookie("itemName");
document.getElementById("itemPrice").innerHTML = getCookie("itemPrice");
document.getElementById("qty").innerHTML = getCookie("qty");

function getCookie(name) {
  var cookies = document.cookie.split(";");

  for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i].trim();

    if (cookie.startsWith(name + "=")) {
      return cookie.substring(name.length + 1);
    }
  }

  return "";
}

Note: The above code is just an example and may need to be modified to fit the specific requirements of the project.