Josh + Paola McFarren

Home | Music | Food | Code | Crafts | Links | Maps | Search:

Ajax Newsletter Subscribe Form


HTML


index.html (line 1)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.     <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  5.     <title>Example</title>
  6.     <script type="text/javascript" src="jquery.js"></script>
  7.     <script type="text/javascript" src="subscribe-submit.js"></script>
  8. </head>
  9. <body>
  10.     <div id="mail-list">
  11.         <form id="mail-list-form" action="subscribe.php" method="post">
  12.             <span id="mail-list-instructions">Sign up to receive emails:</span>
  13.             <input id="mail-list-email" name="email" type="text" maxlength="255" />
  14.             <input id="mail-list-submit" type="submit" value="Add me" />
  15.         </form>
  16.     </div>
  17. </body>
  18. </html>


jQuery


subscribe-submit.js (line 1)
  1. $(document).ready(function(){
  2.     $('form#mail-list-form').submit(function(event){
  3.         var email = $('#mail-list-email').attr('value');
  4.         var validEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  5.         if (!validEmail.test(email)) {
  6.             $('#mail-list-instructions').html('Please enter a valid email address:').css('color','red');
  7.         } else {
  8.             $.ajax({
  9.                 dataType:   'text',
  10.                 data:       $(this).serialize(),
  11.                 type:       $(this).attr('method'),
  12.                 url:        $(this).attr('action'),
  13.                 success: function(){
  14.                     $('#mail-list').html('Thank you for your subscription, your address has been added to our list: '+email);
  15.                 },
  16.                 error: function(){
  17.                     $('#mail-list').html('An error occured submitting the form data.').css('color','red');
  18.                 }
  19.             });
  20.         }
  21.         event.preventDefault();
  22.     });
  23. });


PHP (SMTP with Pear Mail package)


subscribe.php (line 1)
  1. <?php
  2.     require_once "Mail.php";
  3.     $valid_referers  = array(
  4.         'http://example.com/',     
  5.         'http://www.example.com/'
  6.     );
  7.     if ($_SERVER['REQUEST_METHOD'] == 'POST' && in_array($_SERVER['HTTP_REFERER'], $valid_referers)){
  8.         $subscriber_email = $_POST['email'];
  9.         if ($subscriber_email <> '') {
  10.             $headers = array (
  11.                 'To'        => 'List Subscribe <info@example.com>',
  12.                 'From'      => 'List Subscribe <webmaster@example.com>',
  13.                 'X-Mailer'  => 'PHP/'.phpversion(),
  14.                 'Subject'   => 'Mailing list add: '.$subscriber_email
  15.             );
  16.             $message = $headers['Subject'];
  17.             $smtp = Mail::factory('smtp', array (
  18.                 'host'      => 'mail.example.com',
  19.                 'auth'      => true,
  20.                 'username'  => 'example@example.com',
  21.                 'password'  => 'example'
  22.             ));
  23.             $mail_status = $smtp->send(
  24.                 $headers['To'],
  25.                 $headers,
  26.                 $message
  27.             );
  28.             if (!PEAR::isError($mail_status)) {
  29.                 include_once('thank_you.html'); // This is only displayed when the submit fails with AJAX
  30.             } else {
  31.                 header("HTTP/1.1 500 Internal Server Error");
  32.                 echo $mail_status->getMessage();
  33.             }
  34.         } else {
  35.             header("HTTP/1.1 400 Bad Request");
  36.             echo 'Bad Request';
  37.         }
  38.     } else {
  39.         header("HTTP/1.1 400 Bad Request");
  40.         echo 'Bad Referrer';
  41.     }
  42. ?>


PHP (Basic Sendmail)


subscribe.php (line 1)
  1. <?php
  2.     $valid_referers  = array(
  3.         'http://example.com/',     
  4.         'http://www.example.com/'
  5.     );
  6.     if ($_SERVER['REQUEST_METHOD'] == 'POST' && in_array($_SERVER['HTTP_REFERER'], $valid_referers)){
  7.         $email = $_POST['email'];
  8.         if ($email <> '') {
  9.             $to      = 'info@example.com';
  10.             $from    = "From: webmaster@example.com\r\n";
  11.             $subject = 'Mailing list add: '.$email;
  12.             $message = 'Mailing list add: '.$email;
  13.             $mailed  = mail($to_address, $subject, $message, $from);
  14.             if ($mailed) {
  15.                 include_once('thank_you.html'); // This is only displayed when the submit fails with AJAX
  16.             } else {
  17.                 header("HTTP/1.1 500 Internal Server Error");
  18.                 echo 'Failed to send';             
  19.             }
  20.         } else {
  21.             header("HTTP/1.1 400 Bad Request");
  22.             echo 'Bad Request';
  23.         }
  24.     } else {
  25.         header("HTTP/1.1 400 Bad Request");
  26.         echo 'Bad Referrer';
  27.     }
  28. ?>


CategoryCode
Page was generated in 0.3746 seconds