Search code examples
javascriptnewsletternewsletter2go

Newsletter2Go - Form does not work after filling it with javascript?


I try to automatically fill the newsletter2go form with customer data. The form is getting created dynamically with a javascript code which I got from the newsletter2go backend.

But I found out, that the form does not work anymore if I fill it by using javascript!? It works perfectly fine if I fill it manually by hand.

I executed this code from the developer console:

    var inputEmail = document.getElementsByClassName("newsletterInput")[0],
        inputVorname = document.getElementsByClassName("newsletterInput")[1],
        inputNachname = document.getElementsByClassName("newsletterInput")[2],
        selectAnrede = document.getElementsByClassName("newsletterSelect")[0];

    if (inputEmail.value == "") {
        inputEmail.value = 'foo@bar.de';
    }

    if (selectAnrede.value == "") {
        selectAnrede.value = 'm';
    }

    if (inputVorname.value == "") {
        inputVorname.value = 'Edward';
    }
    if (inputNachname.value == "") {
        inputNachname.value = 'Black';
    }

The form is getting filled but if I submit then I get to see Sorry, an error has occurred. Please check your data.

Request Payload at this point: enter image description here

Why does it fail?


If the browser auto-fills the form, then it works btw.


Solution

  • The way the form works is that it only gets updated onchange. That means if you trigger that manually, your code will work.

    Since this seems a bit like a workaround, a cleaner solution would be to implement it similar to a sample form implementation found on the Newsletter2Go homepage.
    The corresponding help article can be found here (only available in German atm).

    BTW, Newsletter2Go offers a recipient profile link that automatically fills the form with customer data. Maybe you can just use that instead.