Search code examples
wordpresscontact-form-7

fetch the field data without post


I'm trying to get the field data from form textarea so i can store it when a user filling the textarea if he reload page or open an other page in same tab and come back to write data his previous work will be there My code is

session_start();
     $_SESSION['textarea-133'] = $_POST["textarea-133"];
     ?>
     <script>

    var a="<php echo $_SESSION['textarea-133']";
    document.getElementById("business_a6").value =a ;
     </script>

Solution

  • You could use window.sessionStorage to save a variable and check if it's there each time the page is loaded.

    window.onload = function(){
        var userText = sessionStorage.getItem('the_user_text');
        if( userText ){
            document.getElementById('the-text-area').innerHTML = userText;
        }
    };
    

    It's up to you if you want to re-save the variable with each keypress or maybe after a setInterval

    document.addEventListener('keypress', (event) => {
      var newText = document.getElementById('the-text-area');
      sessionStorage.setItem('the_user_text', newText);
    });