Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Subscribing to mailing list with AJAX

Monday, July 13th, 2009 by Servage

mailing_list_apiWe have received a few requests about the mailing list API. There has especially been interest in how to integrate the mailing list to an existing website without going through any Servage subscribe/unsubscribe pages. Therefore I have created a very simple sample AJAX integration example, and thought it would be interesting for some of your as well.

You need to include the jQuery library, and please be aware that the email validation is very basic, and please also note that most browsers will not allow the API call because it goes outside the scope of your domain/URL (i.e. it tries to connect to Servage’s domain, instead of yours). In such cases you need to setup a relay-script or similar, but that is not part of this simple tutorial.

<script type="text/javascript">

$(document).ready(function() {
	$('#newsletter form').submit(function(event) {

		// Preventing the default form submit and doing our API call instead
		event.preventDefault();

		// Setting the URL for the API request
		var url = 'http://www.servage.net/apps/ml/?maillist=20262&api=true';

		// Getting the email address
		var email = $('input[name=email]', $(this)).attr('value');

		// Adding the email address to the request
		url = url + '&email=' + email;

		// Is this a "subscribe" or "unsubscribe"? (and adding to the request)
		$('input[name=option]', $(this)).each(function() {
			if ($(this).is('[type=radio]')) {
				if ($(this).is(':checked')) {
					url = url + '&' + $(this).attr('value') + '=true';
				}
			}
		});

		// Adding email to mailing list via API
		if (isValidEmail(email)) {
			$.ajax({
				type: "GET",
				url: url,
				success: function(data, textStatus) {
					// Yes, it was added
					alert('Email address added to mailing list.');
				},
				complete: function (XMLHttpRequest, textStatus) {
					// Whoops, it couldn't be added
					if (textStatus != 'success')
						alert('The email address could not be added (connection error).');
				}
			});
		}
		else
			alert('Please enter a valid email address.');
	});
});

function isValidEmail(email) {
	return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
}
</script>

<div id="newsletter">
	<form method="post" action="#">
		<label for="email">Email address:</label>
		<input type="text" name="email" />
		<label for="email">Subscribe</label>
		<input type="radio" name="option" value="subscribe" checked />
		<label for="email">Unsubscribe</label>
		<input type="radio" name="option" value="unsubscribe" />
		<input type="submit" name="submit" value="Submit" />
	</form>
</div>
Subscribing to mailing list with AJAX, 4.4 out of 5 based on 19 ratings
Categories: Guides & Tutorials

Keywords: , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

7 comments (leave a comment)

For jquery include i prefer to use google’ s

or put it on my own webspace as you never know when the files are moved.

What are the chances of installing a circulating mailing list like Mailman on Servage?
http://www.gnu.org/software/mailman/index.html

I’d do it but one must compile the code and install it using an RPM. Would you be willing to investigate this? It’s part of the Cpanel installation and Fantastico. Even though Servage is very proud of their control panel, there would be no harm in offering Cpanel as well as this one.

Running two or more different control panels in parallel is probably not going to happen any time soon. However, installing a large-scale, professional mailing list applications is not an impossible task. I have relayed your request to our tech department.

Hi..

I have tried https://www.sympa.org/ instead… it have a better support for virtual domains the mailman…

/Dan

drop by. nice blog.

Most of the time I find it adequate to set up some sub folders to sort daily emails as i get them and then archive once a month.

I LOVE THIS BLOG!!!

Leave a comment

You must be logged in to post a comment.