Search code examples
javascripthtmlhidden-field

Is there a way to retrieve hidden input values on another page?


I am trying to take the inputs the customer selects, and add them to hidden fields, to then be able to display them on the cart page.

<form method="post" action="https://omgneonsigns.com/cart/?add-to-cart=5825" name="contentForm" runat="server">

Here is the code for my form, and of course I have my fields setup this way:

<input type="hidden" id="hiddenText" name="hiddenText" value="">

So, what is the next step? I am able to alert the values back to me, so I know they are being held/stored... but I'm not sure how to take them with me to the next page and alert them there?


Solution

  • Firstly, you need to select the hidden input. JavaScript way:

    <input type="hidden" id="hiddenText" name="hiddenText" value="" onchange="myFunction(this.value)">
    
    <script>
        function myFunction(val) {
            let hidden_val = val 
            // now store it using `session`
        }
    </script>
    

    Basically you can do it in many ways:

    1. Use JavaScript session. link: https://www.w3schools.com/jsref/prop_win_sessionstorage.asp
    2. Use php session. link: https://www.w3schools.com/php/php_sessions.asp

    Then get the value in the next page from session.