21 January 2008
You'll need to know how to set up a dynamic site in Dreamweaver CS3 and also have PHP and MySQL set up on your development system.
Intermediate
E-mail newsletters are a very popular method of maintaining an open channel of communication with your site visitors. Newsletters are beneficial for commercial and non-profit sites alike. They have the great benefit of keeping a site's current user base up-to-date on the latest news and offerings from an organization. Moreover, they have the potential for turning site visitors into site customers or donators.
For newsletters to be successful logistically, they have to be easy to subscribe to and—just as importantly—unsubscribe from. With a straightforward entry and exit strategy, many online viewers, leery of incurring additional e-mail spam, will steer clear.
In this article, you'll learn how to create a complete subscribe/unsubscribe application in PHP/MySQL with a standard Dreamweaver CS3 server and just one additional block of code. To use this application, a user signs up for a subscription on a site and receives a confirmation e-mail. The e-mail contains a link to unsubscribe, personalized for the user. In all, you'll need to create five pages to achieve this functionality:
Before you dive into page development, let me say a few words about the preliminary set-up.
This PHP application requires a simple MySQL database and a Dreamweaver connection, in addition to a dynamic PHP site set-up on your testing server.
To define a dynamic site follow these steps:
The Subscriptions database consists of a single table, called subs, which in turn has three data columns: ID, SubEmail, and SubSubscriptions (see Figure 1). The ID column is the primary key and uses an integer format that is automatically incremented. The SubEmail column has a text format and is intended to hold the subscribers e-mail addresses. The final column, SubSubscriptions is a Boolean field, intended to note whether a user is currently subscribed or not—1 indicates subscribed, 0 means unsubscribed. Although this field is not used in this article in this way—unsubscribed records are deleted—it is included as a possible next step. One enhancement of the described technique is to keep the user's e-mail address on file, in case your site offers other e-mail opportunities than just the newsletter, and toggle the SubSubscriptions field as needed.
The SQL file for the Subscriptions database is included in the sample files that accompany this article. You can recreate it on your test server through any number of utilities, including phpMyAdmin, MySQL Control Center, or MySQL Administrator.
After you've established your database, it's time to create a connection to it in Dreamweaver. To do so, follow these steps:
With your PHP dynamic site, MySQL database, and Dreamweaver connection established, you're ready to build the first page of the application.
The subscription page should be simple and clear. You want to keep any barriers to signing up as low as possible. The following example page has a single form field to gather the e-mail address and a submit button, clearly labeled Subscribe Me! (see Figure 3).
The text field is named txtEmail by selecting the page element and entering the name in the provided field on the far left of the Property inspector. The form action property is left blank, so the Dreamweaver Insert Record behavior can add its own code. One other field is inserted into the form: a hidden field labeled SubSubscription with a value of 1. This hidden field will be used by the Insert Record server behavior to populate the identically named field in the database and indicate a subscription has been authorized. Remember that for this Boolean database column, 1 means subscribed.
Note: It's always a good idea to validate your form fields. You can use the Dreamweaver Validate Form behavior or insert the Spry Validation Text Field if you prefer.
To apply the Insert Record server behavior, follow these steps:
The subscription confirmation page is well-named as it carries out its self-named function in two ways. The text on the page lets your site visitors know that they have successfully signed up. It's a great idea to personalize this acknowledgement by repeating your visitors' registered e-mail addresses. A simple Dreamweaver recordset and text binding is needed to accomplish this goal.
You can also use this space to let folks know that a second confirmation has been sent via e-mail with a link for unsubscribing. The text is simple to do in Dreamweaver; the e-mail function takes a bit of handcoding. The e-mail will take advantage of the previously inserted recordset.
Start by adding the recordset to the page by following these steps:
The basic text on the page should be simple and straightforward as shown in Figure 5.
At this stage, the recordset will return all the records and list the last one entered first; to verify this click Test. However, you don't want all the records, you only want one. You can restrict the recordset to the number of records returned with a simple SQL phrase. For this type of customization, you'll need to enter the Recordset Advanced mode.
You can see how your selections in the Simple mode are translated into SQL in the Advanced mode's SQL section.
Now, the recordset returns just the single record, which (because the data is sorted by the automatically incremented ID field in descending order) is the one just inserted.
Now that you have some dynamic data coming into the page, you can personalize it.
The dynamic text is added to the page.
Now that you have a recordset available, you're ready to insert the e-mail code. One of the great benefits of PHP is that it has an easy-to-use e-mail method built in. The mail() function looks like this:
mail(address, subject, message[, header[,parameters]])
The first three arguments (e-mail address, subject, and message) are mandatory. The header strings, which typically contains a from address and can also set the MIME type or list CC: and BCC: addresses, is optional. The parameter argument is also optional and is most frequently used for sending additional arguments to configure the output e-mail.
To make the actual function call readable, I like to set up a series of variables with the values assigned, like this:
$id = $row_rsSubscriber['ID'];
$to = $row_rsSubscriber['SubEmail'];
$subject = "Subscription confirmation";
The first two variables, $id and $to, pull data from the just created recordset that will be incorporated into the e-mail. The third variable, $subject, simply sets the subject line of the message.
Some variables require a lengthy string. I like to use the PHP concatenation operator—the period—to build up the content for such variables, like this:
$body = "<html><body>" .
"<h2>Thank you for subscribing to our newsletter!</h2>" .
"<p>To unsubscribe, click
<a href=\"http://www.relativerealty.com/confirm_unsubscribe.php?ID=" .
$id . "\">here</a>.</p>" .
"</body></html>";
When the variable is fully declared, be sure to conclude with a semi-colon. Although you can send your message in plain text, I thought I'd show you how to send HTML e-mail in this technique. For HTML e-mail, you'll need to add the needed HTML tags to achieve the proper formatting. It's important to note the backslashes in the third code line above; they are used to escape the quotation marks within the <a> tag.
HTML e-mail also requires setting the e-mail header properly. In addition to defining the From address, you'll also need to establish the MIME and content type, like this:
$headers = "From: Subscription Manager
<someoneelse@example.com>\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-type: text/html; charset=UTF-8";
The final portion of the code executes the mail() function, but does it in such a way that it redirects the browser to an error page if a problem is encountered. Here's the code:
if (!mail($to, $subject, $body, $headers)) {
header( 'Location:http://www.relativerealty.com/error_subscribe.php' ) ;
The exclamation mark before the mail() function can be read as not, so if the function cannot be executed, the redirecting header() function comes into play.
Follow these steps to put it all together:
<?php
$id = $row_rsSubscriber['ID'];
$to = $row_rsSubscriber['SubEmail'];
$subject = "Subscription confirmation";
$body = "<html><body>" .
"<h2>Thank you for subscribing to our newsletter!</h2>" .
"<p>To unsubscribe, click <a href=\"http://[YOUR SERVER & PATH HERE]/confirm_unsubscribe.php?ID=" .
$id . "\">here</a>.</p>" . "</body></html>";
$headers = "From: Subscription Manager <someone@mycompany.com>\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-type: text/html; charset=UTF-8";
if (!mail($to, $subject, $body, $headers)) {
header( 'Location: http://[YOUR SERVER & PATH HERE]/error_subscribe.php' ) ;
}
?>
Be sure to substitute the placeholder phrases [YOUR SERVER & PATH HERE] with your own server information and the from address (someone@mycompany.com) with your own from address.
To ensure this portion of the page is complete, you need to create our error page.
The error page informs site visitors that there has been a problem with the process. It is triggered by the failure of the e-mail function. After informing folks of the problem, you can provide them with a link to the subscription page to allow them to re-enter their e-mail addresses and try again (see Figure 9).
You can test your application at this point if you like. After publishing the pages to your web server, go to subscribe.php and enter your e-mail address. Assuming that everything went smoothly, you'll get the confirmation page, and you will shortly receive your e-mail. If the error page is displayed, you know the e-mail function didn't work properly, so start debugging.
Next, build the page that handles unsubscribe requests.
The confirm_unsubscribe page, shown in Figure 10, is very similar to the confirm_subscribe page. Both use recordsets to define a single record and both use a Dreamweaver server behavior to perform a record management function. When folks are subscribing, the server behavior inserts a new record; when they're unsubscribing, a server behavior is used to delete their records.
The structure of the confirm_unsubscribe page will also remind you of its mirror image. Both contain a form with a submit button (see Figure 10). They also contain a hidden form element, but there the similarities ends. The hidden form element is named ID and has a value that retrieves the ID passed from the URL string:
<?php echo $_GET['ID']; ?>
If you don't want to type this code into the Value field of the Hidden Form element Property inspector, you can click Add (+) on the Bindings panel and choose URL Variable from the list. When the URL Variable dialog box appears, enter ID in the Name field and click OK. Then you can just insert ID from the Bindings panel into the Value field and Dreamweaver will write the code for you.
Also, note that although there is space for the e-mail address, there is no form. Because you're just displaying the data and not asking folks to update or insert it, you can use dynamic text.
Follow these steps to insert the recordset:
This filter checks for a URL parameter named ID and restricts the recordset to whatever its matching value is. For example, if the URL that opened this page were something like http://myco.com/confirm_unsubscribe?ID=8, the recordset would just contain the record with an ID of 8.
Now, take advantage of our recordset and bring in some dynamic text
The placeholder for dynamic text appears.
Now add the server behavior, Delete Record.
This setting ensures that the Submit button was clicked. You would choose another entry, like URL Parameter or Session Variable, only if you wanted to delete the record when the page loaded, without user interaction.
This is the hidden form field that you set to the URL parameter, ID.
Just one last page to go—and it's a simple one.
The unsubscribe_complete page is displayed when the record is deleted. The goal here is to let the visitor know that his or her request to unsubscribe has been honored and that the process is now complete. A simple text page, like the one shown in Figure 13, is sufficient.
With the application complete, you should post it to your testing server and begin testing. I'm sure you'll think of other variations and enhancements you can add to this basic application. I hope it's given you some idea of what's possible with Dreamweaver CS3 and PHP as well as providing a solution to extending your web reach.
If you haven't done so, be sure to check out the two-part tutorial, Creating an event registration application in PHP: Building the front end and Building the back end to complement your e-mail confirmation application.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Tutorials and samples |
| 04/23/2012 | Resolution/Compatibility/liquid layout |
|---|---|
| 04/20/2012 | using local/testing server with cs5 inserting images look fine in the split screen but do not show |
| 04/18/2012 | Ap Div help |
| 04/23/2012 | Updating |