Jump to content

Auto-populate the Country field in the Contact Form

Recommended Posts

I built a small landscaping site. All my business is in the US and I would like to either remove the Country field from the contact form or auto-populate it to fill with USA. Is there some custom code for doing this. I am very comfortable with adding custom code to a Squarespace site.

Link to comment
  • 3 weeks later...
On 4/13/2020 at 11:14 AM, MelenMan said:

I built a small landscaping site. All my business is in the US and I would like to either remove the Country field from the contact form or auto-populate it to fill with USA. Is there some custom code for doing this. I am very comfortable with adding custom code to a Squarespace site.

I, too, am looking to resolve this. Once I figure it out, I will update you.

Link to comment
  • 2 weeks later...
  • 4 weeks later...
  • 2 weeks later...

In addition to @andreashirey1914's snippets above, this works for a form in lightbox mode in 7.0:

.field.country {display: none;}

Just make sure you don't have the Address set as a required field, or they won't be able to submit the form since there's no way for them to fill out the Country field.

Edited by Inscape
Link to comment
  • 1 month later...

I tried this and even if it is not required, the form is still asking for a Country.

If you remove the code and your user/buyer enters only Address, City, Zip, even if not required, IT WILL still ask for a Country.

I'm beginning to realise now that Squarespace is just so dumb.

Edited by OliverOrtega
Link to comment
  • 2 months later...

This isn't the world's best coding, but it works on a 7.0 site (might work on 7.1, haven't tested it).

Add this to the page's header injection, or to the site-wide injection (for all pages).

It should auto-populate the country field.

Note:

  • it will only find the first instance of a country field, not multiple forms per page
  • if it doesn't find a country field it will continuously run in the background... in most cases that is not a big deal, but it isn't good form. So probably best to only add this code to pages that have an Address field on them.

Hope that helps someone.

<script>
function fillCountryField() {
  var field = document.querySelector('.field.country .field-element');
  if (field) {
    field.value = 'United States';
  } else {
    setTimeout(fillCountryField, 500);
  }
}
fillCountryField();
</script>

 

Edited by kirkroberts
Link to comment
  • 6 months later...

I'm trying to figure out how to do something similar. I have a simple form on a 7.1 site and I'd like to auto-populate a coupon code into the "Offer Code" field of the form based on data gleaned from the URL. (Nothing about the codes are secret in any way. This is just a convenience thing.)

I'm able to get the coupon code from the URL (e.g., site.com/form-page?code=12345) into my variable via javascript, but it seems like I'm having trouble selecting the input field and pasting the value of that variable. Here's a snippet of the relevant form HTML:

<div id="64ba7865-b8b9-4e6d-8a30-cb7b00c984d3" class="form-item field text required">      
   <label class="title" for="64ba7865-b8b9-4e6d-8a30-cb7b00c984d3-field">
              Offer Code
         <span class="required" aria-hidden="true">*</span>
   </label>
   <input class="field-element text" type="text" id="64ba7865-b8b9-4e6d-8a30-cb7b00c984d3-field" placeholder="ALG-" aria-required="true">
</div>

Any suggestions? I've tried using querySelector on the input id as well as the class. (It's the first instance of an input with that class, so it should be relatively straightforward.)

Link to comment

@TimWilson

Please post the URL for the page your site with the form.

If your site is not public please set up a site-wide password, if you've not already done so.

Post the password here.

Adding a site-wide password is not a security breach. Please read the documentation at the link provided to understand how it works.

We can then take a look at your issue.

Find my contributions useful? Please like, upvote, mark my answer as the best ( solution ), and see my profile. Thanks for your support! I am a Squarespace ( and other technological things ) consultant open for new projects.

Link to comment
  • 10 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.