Problem with Resubmitting Email Newsletter

Problem with Resubmitting Email Newsletter

nikhil1802
Shopify Partner
3 0 0

nikhil1802_1-1742894572583.png

 


When I sign up for the newsletter with my email for the first time, everything appears correct. But when I submit the same email for the second time, the page just refreshes. I want to show an error that says, "You are already our subscriber."
Can it help me with this?

 

 

 

Replies 3 (3)

Dotsquares
Shopify Partner
390 24 52

Hi @nikhil1802 

You can modify Shopify's newsletter signup form to show an error message.

In the Theme go to Edit Code and replace the existing code with below. Please let me know if this works- 

<form method="post" action="{{ routes.root_url }}contact#ContactForm" id="newsletterForm">
    <input type="email" name="contact[email]" id="newsletterEmail" placeholder="Enter your email" required>
    <button type="submit">Subscribe</button>
    <p id="newsletterError" style="color: red; display: none;">You are already our subscriber.</p>
</form>

<script>
document.getElementById("newsletterForm").addEventListener("submit", function(event) {
    event.preventDefault(); // Stop the form from submitting immediately

    var email = document.getElementById("newsletterEmail").value;

    fetch('/contact?email=' + encodeURIComponent(email))
    .then(response => response.text())
    .then(data => {
        if (data.includes("already subscribed")) { // Adjust this based on your theme's error message
            document.getElementById("newsletterError").style.display = "block";
        } else {
            this.submit(); // Submit the form if email is not found
        }
    })
    .catch(error => console.error('Error:', error));
});
</script>

 

Dotsquares Ltd


Problem Solved? ✔ Accept and Like solution to help future merchants.


Shopify Partner Directory | Trustpilot | Portfolio
nikhil1802
Shopify Partner
3 0 0
Hi

Thank you for you response

Please give me a few minutes. i will check that.

Best regards
Nikhil

nikhil1802
Shopify Partner
3 0 0

It's not working