Ask Your Question
3

What is the process of obtaining ItemCode, ItemName, ItemPrice and Qty from the user, saving them as cookies, and subsequently presenting them on the following webpage?

asked 2022-02-01 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-15 14:00:00 +0000

huitzilopochtli gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-02-01 11:00:00 +0000

Seen: 7 times

Last updated: Apr 15 '22