Use AJAX and PHP to Build your Mailing List

Notice: This is a discussion thread for comments about the SitePoint article, Use AJAX and PHP to Build your Mailing List.


Is there a link to a demo?

The code archive is at the top of the article to set it up on your server and try out. You’ll just need to set up the table in your MySQL database.

There is also now a demo you can try out. Demo Mailing List Sign Up

Why the code doesn’t prevent duplication! users can submit their emails couple of times!

You can prevent sign up duplicates by preceding the INSERT statement with a SELECT looking for the address in the database. You can use a conditional to check if the SELECT statement returned any records. If so, we can return a message letting the user know they’ve already signed up. Here is what that might look like:


$addresscheck = mysql_query("SELECT * FROM mailinglist WHERE email='" . $address . "'");
	  
if( mysql_num_rows($addresscheck) != 0 ){
	$message = "Address already signed up";
}else{
	// Insert email address into mailinglist table 
	$result = mysql_query("INSERT INTO mailinglist SET email='" . $address . "'");
	if(mysql_error()){
		$message = "<strong>Error</strong>: There was an error storing your email address.";
	} else {
		$message = "Thanks for signing up!";
	}
		  
} // End Check for Duplicate Address

Or check this out: http://dev.mysql.com/doc/refman/5.0/en/replace.html

You could use REPLACE instead of insert, to automatically check for duplicates… You’d have to make the email field UNIQUE though (which isn’t hard…)

Nice article, thanx!

If you are interested in extending this mailing list sign up system to receive more user input, or even apply these concepts to other contexts, you may find this helpful: Extending the Ajax Mailing List Sign Up System

The code download gives a 404

I think the example was a good one. Two issues I had:

  1. The storeAddress.php uses short tags to open. I have them disabled on my server, therefore I had to add the ‘php’. The other pages used ‘php’.
  2. Justfying a large prototype library for a few small functions. It could have been avoided by using some DOM methods. I just didn’t think prototype was completely necessary for the small task.

Other than that, I liked it :slight_smile:

File size is a common criticism of Prototype, and I think a valid one. I like it for this purpose because it greatly simplifies the potential complexity of wrangling Ajax calls/responses, especially if you are new to it. I am also making an assumption that it would be used for other tasks in a site as well.

It’s great to question the use of a framework like this, though, rather than getting caught up in its hype, because a 52k addition to a page for a simple task may not fit the bill for your target audience and site goals.

The latest Prototype is 46 kb, and each new version seems to be about 2 kb lees than the previous. I think that this is a very reasonable amount of code for the work that it does, and I’m pretty certain that it could be reduced to 40kb if the application demanded it, with the removal of comments and whitespace.

Congratulations to the Prototype developers, and to Aarron and all at sitepoint for the info.

My Javascript file includes 6 ajax functions, including login/logout and it’s only 3kb in size.

Nice script!

Am I right in thinking it doesn’t check for duplicate email addresses?

As I’ve expanded the form to include other fields, I’d like to use Prototype’s Form.serialize to make the variables. But when I replace this:

var pars = 'address=' + escape($F('address'));

with this:

var pars = Form.serialize(addressForm)

then the form submits to wherever is specified in the action attribute. Why is that, and how can I work around it?

@bas: I’d love to see that file. Mind sharing it?

Thanks in advance.

Dotan

Regarding preventing duplicates, I saw the mention of this above.

But how would you impliment this on the storeAddress.php code exactly?

It keeps giving me errors. (eg
Warning: mysql_query() [function.mysql-query]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /home/.canicula/jamesgreig/sneakergeeks.com/inc/storeAddress.php on line 9)

Articles and examples in ‘sitepoint’ is really very helpful in self learning and using then in our own business. The latest technologies used will surely enhance the knowledge base required for an individual to grow

Would anyone be able to point me into the right direction of adding the following three functionalities to this list:

  1. Send a confirmation email to the user to confirm their registration to the mailing list.

  2. Unsubscribe Function along with confirmation.

  3. Actually sending out emails to the users that signed up. ie: using the mailing list to send out newsletters.

4Seen, my recommendation to achieve all of the functionality you are looking for is to tie in to a mailing list service. Mail Chimp is the one I personally like best, and they have an API along with a PHP class to simplify the process. To learn more you can check out [URL=http://www.mailchimp.com/resources/guides/subscriber_api.phtml]http://www.mailchimp.com/resources/guides/subscriber_api.phtml

You can use this Ajax approach to send the sign up to Mail Chimp rather than store it on your server in a database.

is it possible to do this with another mailing list script?

it works very good
thank you Aarron

Regards,
Nima Fatemi