Appreciate your help on what I'm sure is a real basic head-slapper. I'm setting up a simple web front-end so that I can check if an email address is already a subscriber to an existing MailChimp list. For now I'd like to just use basic HTML & JavaScript.
My MailChimp API Key checks out, and I can run queries that only require string data, but the relevant API call requires a struct for email address(es), and I keep getting an error in any variation of the code below. I've gone as far as installing node.js and just running that from the command line and the query does work with the same data, so I'm sure I'm just this close, but need your help to get over the hump.
Here is the relevant snippet of the script and form. As written, I receive the following error message in reply: Validation error: {\"email\":\"Please enter a struct\/associative array\"}"}
<script type="text/javascript">
function collectAndSubmit() {
var emailAddy = document.getElementById("emailAddy").value;
var JSONemail = {"email":
{
"email": emailAddy
}
};
document.getElementById("email").value = JSONemail;
document.getElementById("apikey").value ="M*Y*A*P*I*K*E*Y"
document.testform.submit();
}
</script>
</head>
<body>
<form action="https://us2.api.mailchimp.com/2.0/helper/lists-for-email" method="post" name="testform">
Email Address: <input type="text" id="emailAddy" name="emailAddy" />
<input type="hidden" id="apikey" name="apikey" />
<input type="hidden" id="email" name="email" />
<br>
<input type="button" value="Check Email" onclick="collectAndSubmit()" />
</form>
</body>
</html>
Thanks in advance for your help.
Your form is incorrect. You can take a look at their documentation. You have to setup what they call an email struct which is really an array. Your email input attribute should look like <input type="text" id="email" name="email[email]">.
Notice the email[email] it will make the data look like "email": {"email": "input value"}
Also if your api key is supposed to be a secret then placing it hidden in the html form is not a good idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With