Ensure empty values are not submitted

Name attributes specify the name of particular html elements. In the case of gift messages obtained through Giftship, your customer may have filled in fields with name attributes such as ‘To’, ‘From’, and ‘Message’.

If you would like to remove these from being submitted, when empty, disabling the field before the form is submitted is the best way to do so.

To remove empty values from the form go to Online Store -> Themes -> Your Theme -> theme.liquid, and insert the following code above the closing </body> tag:


 <script>

    (function() {

      var GCP = {

        init: function() {

          GCP.assignElements();
          GCP.events();
          
        },

        elements: {

          productForm: null,
          formSubmitButton: null,
          
        },
        
        state: {

          form_handled: false
          
        },

        assignElements: function() {

          GCP.elements.productForm = document.querySelector('form[action*="cart/add"]');
          GCP.elements.formSubmitButton = document.querySelector('button[id*="AddToCart--product-template"]');
          
        },

        events: function() {

          GCP.elements.productForm.addEventListener('submit', GCP.functions.handleFormSubmit);
          
        },

        functions: {

          handleFormSubmit: function(event) {

            if (GCP.state.form_handled === true) {
              return true;
            }
            
            GCP.state.form_handled = true;

            GCP.functions.removeAttributes();

            GCP.elements.formSubmitButton.click();
            
            event.preventDefault();
            
          },

          removeAttributes: function() {

            var giftshipInputs = GCP.elements.productForm.querySelectorAll('.gs__product-element');

            giftshipInputs.forEach(function(el) {

              if (el.value !== "") return;

              el.setAttribute('disabled', 'disabled');
              
            });
            
          }
          
        }
        
      }

      GCP.init();
      
    })();
    
  </script>

Can't find the answer in our documentation?
Contact Support