Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

Wordpress empties form fields when refresh page

Wordpress WooCommerce When you fill in form field data and for whatever reason you need to refresh the page, you loose all of your form field data.  Is there anyway to override the Core Wordpress Functionality to reload empty fields?

I tried something like this:
    window.onload = function() {



        // If values are not blank, restore them to the fields
        var firstName = sessionStorage.getItem('firstName');
        $('#billing_first_name').val(firstName);

     

        var message= sessionStorage.getItem('message');
        $('#order_comments').val(message);
    }

    // Before refreshing the page, save the form data to sessionStorage
    window.onbeforeunload = function() {
        sessionStorage.setItem("firstName", $('#billing_first_name').val());
        sessionStorage.setItem("message", $('#order_comments').val());
    }

Open in new window


Go here and pick either product: http://slideswizard.fireflie.webfactional(DOT)com/pricing/

Go to the checkout page fill in billing or customer notes and then upload a file.  It will clear the form field Data
Avatar of Andrei Filonov
Andrei Filonov
Flag of Canada image

There is an error message in the console:

Uncaught TypeError: $ is not a function  at window.onload

Open in new window


Basically this will kill your calls to store / load session data.

Solution: replace $ with jQuery, e.g.

window.onload = function() {

        // If values are not blank, restore them to the fields
        var firstName = sessionStorage.getItem('firstName');
        jQuery('#billing_first_name').val(firstName);

        var message= sessionStorage.getItem('message');
        jQuery('#order_comments').val(message);
    }

    // Before refreshing the page, save the form data to sessionStorage
    window.onbeforeunload = function() {
        sessionStorage.setItem("firstName", jQuery('#billing_first_name').val());
        sessionStorage.setItem("message", jQuery('#order_comments').val());
    }

Open in new window


P.S. Please never change WP core files :D
Avatar of Robert Granlund

ASKER

That did not work.

I basically need to store the Custom Message.  If you upload a file, once it has uploaded it clears the form, so any information that a customer enters into the form is lost.  It is a pain for the customer.
where exactly is your code located? Right now there is no event attached to window.onbeforeunload...
ASKER CERTIFIED SOLUTION
Avatar of Andrei Filonov
Andrei Filonov
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial