├── CONTRIBUTING.md ├── README.md ├── assets ├── css │ └── style.css ├── images │ ├── arrow.png │ └── loading.gif └── js │ └── lib.js ├── create_table.sql ├── inc ├── Database.class.php └── Newsletter.class.php ├── index.html ├── screenshot.png └── send.php /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | 1. Fork it! 4 | 2. Create your feature branch: `git checkout -b my-new-feature` 5 | 3. Commit your changes: `git commit -m 'Add some feature'` 6 | 4. Push to the branch: `git push origin my-new-feature` 7 | 5. Submit a pull request :D 8 | 9 | English is the universal language nowadays, so please don't create or comment on issues using another language. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ajax PHP MySQL Newsletter 2 | ========================= 3 | 4 | A simple newsletter sign-up form using Ajax, PHP and MySQL. 5 | 6 |  7 | 8 | How to use 9 | ----------------- 10 | 11 | * Run [create_table.sql](create_table.sql) file to create the structure of the database. 12 | * Open [Database.php](inc/Database.class.php) class file and fill the variables with data from your database. 13 | * Enjoy. 14 | 15 | Contributing 16 | ----------------- 17 | 18 | Check [CONTRIBUTING.md](CONTRIBUTING.md) for more information. 19 | -------------------------------------------------------------------------------- /assets/css/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 62.5%; 3 | background-color: #64c6af; 4 | } 5 | 6 | #newsletterform { 7 | font-family: 'Raleway', sans-serif; 8 | margin: 0 auto; 9 | max-width: 780px; 10 | min-width: 320px; 11 | display: block; 12 | text-align: center; 13 | letter-spacing: 1px; 14 | position: relative; 15 | } 16 | 17 | #newsletterform .wrap { 18 | padding: 50px 40px; 19 | } 20 | 21 | #newsletterform h3 { 22 | font-family: 'Raleway', sans-serif; 23 | display: block; 24 | font-size:26px; 25 | font-weight:400; 26 | line-height:1.2; 27 | color:#fff; 28 | text-transform: uppercase; 29 | text-align: center; 30 | padding-bottom:20px; 31 | margin: 0 auto 30px auto; 32 | border-bottom:1px solid #fff; 33 | width:60%; 34 | } 35 | 36 | #newsletterform p { 37 | font-size:14px; 38 | line-height:26px; 39 | margin-bottom:25px; 40 | color:#fff; 41 | } 42 | 43 | #newsletterform input { 44 | font-family: 'Raleway', sans-serif; 45 | font-size: 16px; 46 | font-weight:500; 47 | display: block; 48 | width:55%; 49 | background:#fff; 50 | color:#888; 51 | padding:15px; 52 | text-align: center; 53 | text-transform: uppercase; 54 | margin: 0 auto 12px auto; 55 | border:2px solid #fff; 56 | outline:0; 57 | border-radius: 3px; 58 | -webkit-box-sizing: border-box; 59 | -moz-box-sizing: border-box; 60 | box-sizing: border-box; 61 | } 62 | 63 | #newsletterform input:focus { 64 | border:2px solid #ddd; 65 | } 66 | 67 | #newsletterform input[type="submit"] { 68 | background:#f77156; 69 | color:#fff; 70 | width:30%; 71 | padding:13px; 72 | cursor: pointer; 73 | margin-bottom:0; 74 | border:none; 75 | } 76 | 77 | #newsletterform input[type="submit"]:hover { 78 | background:#F8CA4D; 79 | color:#444; 80 | } 81 | 82 | #newsletterform .arrow { 83 | display: inline-block; 84 | background: url("../images/arrow.png") no-repeat; 85 | width: 139px; 86 | height: 48px; 87 | position: absolute; 88 | left: 120px; 89 | top: 290px; 90 | } 91 | 92 | #newsletterform #response { 93 | color: #fff; 94 | font-size: 1.4em; 95 | height: 25px; 96 | margin: 0 auto; 97 | padding-top: 15px; 98 | text-align: center; 99 | width: 320px; 100 | } 101 | 102 | #newsletterform #response p { 103 | margin: 0 104 | } 105 | 106 | #newsletterform .loading { 107 | background: url("../images/loading.gif") no-repeat center 20px; 108 | } 109 | 110 | @media only screen and (max-width:769px) { 111 | 112 | #newsletterform .wrap { 113 | padding: 40px 25px; 114 | } 115 | 116 | #newsletterform .arrow { 117 | display: none; 118 | } 119 | 120 | #newsletterform input { 121 | width: 100%; 122 | margin-bottom: 7px; 123 | } 124 | 125 | #newsletterform input[type="submit"] { 126 | width: 100%; 127 | padding: 16px; 128 | } 129 | 130 | } 131 | 132 | @media only screen and (max-width:600px) { 133 | 134 | #newsletterform h3 { 135 | width: 100%; 136 | } 137 | 138 | } -------------------------------------------------------------------------------- /assets/images/arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinceladasdaweb/Ajax-PHP-MySQL-Newsletter/e64f1a864cca1564a65d70ff291b79ec91ec70bf/assets/images/arrow.png -------------------------------------------------------------------------------- /assets/images/loading.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pinceladasdaweb/Ajax-PHP-MySQL-Newsletter/e64f1a864cca1564a65d70ff291b79ec91ec70bf/assets/images/loading.gif -------------------------------------------------------------------------------- /assets/js/lib.js: -------------------------------------------------------------------------------- 1 | $(document).ready(function () { 2 | $('#newsletter').submit(function () { 3 | var $this = $(this), 4 | $response = $('#response'), 5 | $mail = $('#signup-email'), 6 | testmail = /^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/, 7 | hasError = false; 8 | 9 | $response.find('p').remove(); 10 | 11 | if (!testmail.test($mail.val())) { 12 | $response.html('
Please enter a valid email
'); 13 | hasError = true; 14 | } 15 | 16 | if (hasError === false) { 17 | 18 | $response.find('p').remove(); 19 | $response.addClass('loading'); 20 | 21 | $.ajax({ 22 | type: "POST", 23 | dataType: 'json', 24 | cache: false, 25 | url: $this.attr('action'), 26 | data: $this.serialize() 27 | }).done(function (data) { 28 | $response.removeClass('loading'); 29 | $response.html(''+data.message+'
'); 30 | }).fail(function() { 31 | $response.removeClass('loading'); 32 | $response.html('An error occurred, please try again
'); 33 | }) 34 | } 35 | 36 | 37 | return false; 38 | }); 39 | }); -------------------------------------------------------------------------------- /create_table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE `signups` ( 2 | `signups_id` int(10) NOT NULL AUTO_INCREMENT, 3 | `signup_email_address` varchar(250) DEFAULT NULL, 4 | `signup_date` datetime DEFAULT NULL, 5 | PRIMARY KEY (`signups_id`) 6 | ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 7 | -------------------------------------------------------------------------------- /inc/Database.class.php: -------------------------------------------------------------------------------- 1 | getMessage()); 21 | } 22 | } 23 | return self::$cont; 24 | } 25 | 26 | public static function disconnect() { 27 | self::$cont = null; 28 | } 29 | } -------------------------------------------------------------------------------- /inc/Newsletter.class.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 31 | $existingSignup = $pdo->prepare("SELECT COUNT(*) FROM signups WHERE signup_email_address='$email'"); 32 | $existingSignup->execute(); 33 | $data_exists = ($existingSignup->fetchColumn() > 0) ? true : false; 34 | 35 | if (!$data_exists) { 36 | $sql = "INSERT INTO signups (signup_email_address, signup_date) VALUES (:email, :datetime)"; 37 | $q = $pdo->prepare($sql); 38 | 39 | $q->execute( 40 | array(':email' => self::$email, ':datetime' => self::$datetime)); 41 | 42 | if ($q) { 43 | $status = "success"; 44 | $message = "You have been successfully subscribed"; 45 | } else { 46 | $status = "error"; 47 | $message = "An error occurred, please try again"; 48 | } 49 | } else { 50 | $status = "error"; 51 | $message = "This email is already subscribed"; 52 | } 53 | } 54 | 55 | $data = array( 56 | 'status' => $status, 57 | 'message' => $message 58 | ); 59 | 60 | echo json_encode($data); 61 | 62 | Database::disconnect(); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |