Su cribe.a
<%@ Language=JavaScript %>
<!--#include file = "include/SetGlobals.a quot;-->
<!--#include file = "include/D ath.a quot;-->
<%
// output relevant meta tags
Init( " u criptio quot; );
// output common top of page
Header( '<a href="work.a quot gt;Work</a> --> Su cription', 3 );
// output page content
Content ( );
// output common bottom of page
Footer( );
%>
<% /* standard page elements */ %>
<!--#include file = "utils/Init.a quot;-->
<!--#include file = "utils/Database.a quot;-->
<!--#include file = "utils/Header.a quot;-->
<!--#include file = "utils/Footer.a quot;-->
<%
// ============================================
// the content of this page
// ============================================
function Content ( )
{
Out ( '<td width="20%" gt am lt;/td>' );
Out ( '<td width="60%" gt;' );
// if the form has an email addre , validate it first
// so that if it fails we can show the form to fix
var sEmail = " quot
var ubmitted = (Request.Form.Count > 0);
// has the form been submitted?
if ( ubmitted )
{
// get the email addre from the form...
Email = " quot; + Request.Form ( "email" );
// validate the email addre and moan if it fails
if ( !IsValidEmail ( sEmail ) )
{
Out ( '<h5> lt;font color="red" gt quot;' + sEmail + '" <i>a ear lt;/i> to be an invalid email addre - please try again!</font> lt;/h5>' );
Out ( '< gt lt;font color="red" gt;If you disagree, please <a href="Contact.a quot gt;contact me</a> directly.</font> lt gt;' );
// pretend the form ha '\t been sent yet
ubmitted = false;
}
}
// show the form if not submitted yet
if ( ! ubmitted )
{
Out ( 'If you\'re interested in hearing whenever a new article is posted, or an existing one is updated, type in your email addre below and hit < gt u cribe!</ gt;' );
Out ( '< gt;Whenever you want to stop receiving my emails, gue what? That\'s right, enter your email addre and hit < gt;U u cribe</ gt;...' );
Out ( '< gt lt;i>Your email addre will never sold to or otherwise used by any third party, just me.</i>' );
// here's the form tag. the action attribute is the name of
// the file that will be called with the a wer - in this case
// it's the same page. the method can be " ost" to send the
// form data 'behind the scenes' or "get" to a ending the
// data to the URL in the style page.a ?data1=a&am data2=b
//
// use post most of the time - it's neater and "get" is limited
// in the amount of data that can be sent.
Out ( '<center> lt;form action=" u cribe.a quot; method=" ost" gt;' );
// another table to line up the titles and i uts
Out ( '<table border="0" cellpadding="0" gt;' );
Out ( '<tr> lt;td align="right" valign="to quot gt;' );
Out ( 'Email:' );
Out ( '</td> lt;td align="left" valign="to quot gt;' );
// a simple text box. we'll reference it with the name " ame"
// and show 22 characters on the form. use the maxlength
// attribute to set the maximum characters they can enter.
// use value=" ome text" to pre-fill the i ut with data.
//
// IMPORTANT! using names that are commonly used by
// other web sites has a big advantage to the user - IE
// will drop down a list of previous a wers, which they
// can usually pick from rather than type in. Think about this.
Out ( '<i ut type="text" name="email" size="31" value="' + sEmail + '" gt lt;/i ut>' );
Out ( '</td> lt;/tr>' );
Out ( '<tr> lt;td align="right" valign="to quot gt;' );
Out ( '&am ' );
Out ( '</td> lt;td align="left" valign="to quot gt;' );
// type='submit" provides a submit button to perform the
// form action. the button says " ubmit" unle you override
// with the value attribute.
Out ( '<i ut type=" ubmit" name="actio quot; value=" u cribe" gt lt;/i ut> am lt;i ut type=" ubmit" name="actio quot; value="U u cribe" gt lt;/i ut>' );
Out ( '</td> lt;/tr>' );
Out ( '</table>' );
Out ( '</form> lt;/center>' );
}
else
{
var sAction = " quot; + Request.Form ( "actio quot; );
if ( sAction == " u cribe" )
AddEmail ( sEmail ) ;
else
RemoveEmail ( sEmail );
Out ( '< gt;' );
}
Out ( 'Do you want to see how this form adds and removes addre es to my database? All the source code is just a click away!' );
Out ( '< gt lt;center> lt;a href=" howSource.a ? page=Su cribe" gt lt;img src=http://www.knowsky.com/"images/source.gif" border=0> lt;/a> lt;/center>' );
Out ( '< gt;In <a href="MailToList.a quot gt art 2</a> see how I wrote a form to mail all my su cribers...' );
Out ( '</td>' );
Out ( '<td width="20%" gt am lt;/td>' );
}
// ============================================
// validate email addre
// ============================================
function IsValidEmail ( sEmail )
{
// regular expre ion courtesy of ed.courtenay@nationwidei .net
// I won't even pretend that I've read through this yet!
if ( sEmail.search ( /\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0- 9]+)*\.[A-Za-z]{2,5}/ ) != -1 )
return true;
else
return false;
}
// ============================================
// add email to database
// ============================================
function AddEmail ( sEmail )
{
// open the co ection
DBInitCo ection ( );
// first see if they are already su cribed
var QL = 'SELECT Email FROM MailingList WHERE Email="' + sEmail + '" ';
DBGetRecords ( QL );
if ( !oRecordSet.EOF )
{
Out ( '<h5> lt;font color="red" gt;' + sEmail + ' is already su cribed to my mailing list!</font> lt;/h5>' );
retur
}
// this section needs more work - what should be done is that an email is
// sent to the email addre , and only added to the database when we
// get a reply. that way we know the addre is valid and the recipient
// really wants to join the list. for now though, we'll add to the db now.
QL = 'I ERT INTO MailingList (Email) VALUES ("' + sEmail + '");';
oCo ection.Execute( QL );
// free the co ection
DBReleaseCo ection ( );
Out ( sEmail + ' has been succe fully su cribed to my mailing list. ' );
Out ( '< gt;You will now receive an email whenever I write new articles, or if I make an important update to any.' );
Email ( 'Joined the ShawThing mailing list', sEmail, 'You have succe fully su cribed to the mailing list at ShawThing. If you didn\'t request this please reply to this email, or visit http://www.shawthing.com/su cribe.a to u u cribe.\n\nThank you.\n\nJames Shaw\nhttp://www.shawthing.com/' );
}
// ============================================
// remove email from database
// ============================================
function RemoveEmail ( sEmail )
{
// open the co ection
DBInitCo ection ( );
// first see if they are already su cribed
var QL = 'SELECT Email FROM MailingList WHERE Email="' + sEmail + '" ';
DBGetRecords ( QL );
if ( oRecordSet.EOF )
{
Out ( '<h5> lt;font color="red" gt;' + sEmail + ' i \'t su cribed to my mailing list!</font> lt;/h5>' );
retur
}
// delete from the database
QL = 'DELETE FROM MailingList WHERE Email="' + sEmail + '" ';
oCo ection.Execute( QL );
// free the co ection
DBReleaseCo ection ( );
Out ( sEmail + ' has been succe fully removed from my mailing list. ' );
Out ( '< gt;You have been sent a confirmation email, but after that you will not receive any more emails.' );
Email ( 'Removal from ShawThing mailing list', sEmail, 'You have been succe fully removed from the mailing list at ShawThing. If you didn\'t request this please reply to this email, or visit http://www.shawthing.com/su cribe.a to re-su cribe.\n\nThank you.\n\nJames Shaw\nhttp://www.shawthing.com/' );
}
// ============================================
// email me!
// ============================================
function Email ( ubject, sEmail, sMe age )
{ // send an email to the addre just to confirm what just ha ened
var oMail = Server.CreateObject ( "CDONTS.NewMail" );
// setup the mail
oMail.From = 'DB@shawthing.com';
oMail.To = sEmail;
oMail.Importance = 1;
oMail.Subject = ubject;
oMail.Body = sMe age;
// send it
oMail.Send ( );
// release object
oMail = null;
}
%>
utils/Database.a
<%
// globals
var oCo ectio
var oRecordSet;
var sCo ectio
// ============================================
// example usage:
// DBInitCo ection ( );
//
// var QL = " ELECT * FROM Somewhere"
//
// DBGetRecords ( QL );
//
// ...use oRecordSet
//
// DBReleaseRecords ( ) // optional step
//
// DBReleaseCo ection ( );
// ============================================
// ============================================
// initializes database variables for first use on page
// ============================================
function DBInitCo ection ( )
{
// don't open it again if already opened!
if ( sCo ection != undefined )
retur
// get co ection object
oCo ection = Server.CreateObject( 'ADODB.Co ection' );
// get the database co ection string
// use Ma ath to make relative path into physical path
Co ection = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + Server.Ma ath ( sD ath );
// open the co ection
oCo ection.Open( sCo ection );
// as an attempt at optimization we now open
// the recordset here, not in DBGetRecords()
oRecordSet = Server.CreateObject ( 'ADODB.Recordset' );
}
// ============================================
// tidies up after DBInitCo ection
// ============================================
function DBReleaseCo ection ( )
{
// don't release the co ection if not co ected!
if ( sCo ection == undefined )
retur
// as an attempt at optimization we now close
// the recordset here, not in DBReleaseRecords()
if ( oRecordSet.State != 0 )
oRecordSet.Close();
oRecordSet = undefined;
oCo ection.Close();
oCo ection = undefined;
Co ection = undefined;
}
// ============================================
// executes the pa ed in SQL statement
// and retur the oRecordSet object
// ============================================
function DBGetRecords ( QL )
{
// remember that this can fail if pa ed garbage, and hence
// 'oRecordSet' will already be 'closed'
oRecordSet = oCo ection.Execute( QL );
}
// ============================================
// tidies up after DBGetRecords
// ============================================
function DBReleaseRecords ( )
{
// IMPORTANT: THIS FUNCTION INTENTIONALLY BLANK
// as an attempt at optimization we now open/close
// the recordset with the co ection, not separately
// so all code was moved to DBReleaseCo ection.
// it is recommended that you still call this function as soon
// as the recordset is finished with.
// note that it is a umed by the caller that it is legal
// to call DBReleaseCo ection without calling this function
}
%>
an>


