@Viktor_Iwan is correct, in order to add a contact to your list using ajax you can do it a couple of ways. If you are using jQuery it might look something like this:
var apiUrl = "http://account.api-us1.com"; // get this from settings
var apiString = apiUrl + "/admin/api.php";
var apiKey = ""; // WARNING! probably want to keep this off of client-side calls!
var contact = {
"api_action": "contact_sync",
"api_key": apiKey,
"email": "test@example.com",
"first_name": "Test", // optional
"last_name": "User" // optional
// additional fields from http://www.activecampaign.com/api/example.php?call=contact_sync
};
$.post(apiString, contact, (response) => {
console.log("Response: ", response);
});
I probably should have thought about that a bit better. If you are only subscribing a contact to your list, and keeping the api key safe is a concern, it probably isnt the best idea to use the contact_sync API endpoint.
There are a few ways to do it, the one is you could send a POST request via AJAX to your own PHP server/script for processing on the server, and then make the contact_sync call serverside.
I’ll talk to the devs and see what else they recommend.
Using PHP would help as I am currently using a local development nad cross origin is making the javascript method impossible.
I have been trying to wrap my head around using the PHP method displayed in the API Docs, but beyond setting variables and working with WordPress me and PHP have never gotten on. I really appreciated any help you can give me on this.
One option that is always available, is to use the form html that is provided from the form builder, and strip out all the included javascript. You can then intercept the form submit event, do whatever data processing you need to do, and then submit the data via ajax. That should solve CORS issues and the API key security issues.
If it were me, I would build the form using just the email field (minimum necessary field) using the form builder and then pull just the html from that and handle the submission via event.preventDefault() and AJAX.
I would keep all of the form HTML it is there to help prevent against bots etc.
Of course you could also use a form-builder plugin like Gravity Forms or Contact Forms 7 etc if you are on WordPress. I am not familiar with the details of how those work though.
I’ll have a wrestle with this tomorrow and see how successful I am. Thanks for all your helpful suggestions! I think in the end I’d rather try and get the PHP method working, as it is the most flexible and secure (I think).