Skip to content

Instantly share code, notes, and snippets.

@rbarros
Created May 23, 2012 21:42
Show Gist options
  • Save rbarros/2778011 to your computer and use it in GitHub Desktop.
Save rbarros/2778011 to your computer and use it in GitHub Desktop.
Newsletter
/*!
* Newsletter JavaScript Library v0.5.12
* http://public.ramon-barros.com/
*
*
* Copyright (c) 2012 Ramon Barros and other contributors,
* http://ramon-barros.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Includes jQuery
* http://jquery.com/
* Copyright 2012,
* http://jquery.org/license/
*
* Date: Wed Jul 27 13:17:00 2012 -0300
*/
var Newsletter = {
name:'',
email:'',
lang:'pt',
status:'',
json:'',
msg:{
'pt':
{
'name':'Campo nome é obrigatório',
'email':'Digite um email válido',
'success':'Cadastro efetuado com sucesso',
'error':'Não foi possivel efetuar o cadastro',
'register':'Email já cadastrado !'
},
'en':
{
'name':'Field name is required',
'email':'Please enter a valid email address',
'success':'Registration completed successfully',
'error':'Unable to make the register',
'register':'Email já cadastrado !'
},
'es':
{
'name':'Nombre del campo es obligatorio',
'email':'Por favor, introduzca una dirección válida de correo electrónico',
'success':'Registro completado con éxito',
'error':'No se puede realizar el registro',
'register':'Email já cadastrado !'
}
},
ajax:function(){
//AjaxEz.open('POST','includes/newsletter_ajax.php?action=cadastro&nome='+this.name+'&email='+this.email,false,'json');
$.ajax({url: "includes/ajax/newsletter.php",type: "POST",async:false,data: {action:"cadastro",nome:this.name,email:this.email},dataType: "json",success: function(json){ Newsletter.json = json; } });
},
registerEmail:function(){
var message = ' ';
var success = 'success';
var error = 'error';
var register = 'register';
if(this.checkEmail() && this.checkName()){
this.ajax();
if(this.json.error===true){
if(this.json.cadastro===true){
message = message + '<p class="error">'+this.msg[this.lang][register]+'</p>';
}else if(this.json.cadastro===false){
message = message + '<p class="error">'+this.msg[this.lang][error]+'</p>';
}
}else{
if(this.json.cadastro===false){
message = message + '<p class="success">'+this.msg[this.lang][success]+'</p>';
}
}
console.info(message);
this.messageFields(message);
}
},
checkEmail:function(){
var check = this.email.match(/^[A-z0-9._%+\-]+@[A-z0-9.\-]+\.[A-z]{2,4}/);
if(!check){
return false;
}else{
return true;
}
},
checkName:function(){
if(this.name.length>0){
return true;
}else{
return false;
}
},
checkFields:function(){
var name = 'name';
var email = 'email';
var message=' ';
if(!this.checkName()){
message = message + '<p class="newsletter_error_name">*'+this.msg[this.lang][name]+'</p>';
}
if(!this.checkEmail()){
message = message +'<p class="newsletter_error_email">*'+this.msg[this.lang][email]+'</p>';
}
if(!this.checkName() || !this.checkEmail()){
this.messageFields(message);
}else{
return true;
}
},
message:function(){
var success = 'success';
var error = 'error';
$('div#msg').fadeIn('slow');
if(this.status){
this.$('msg').innerHTML = '<p class="success">'+this.msg[this.lang][success]+'</p>';
}else{
this.$('msg').innerHTML = '<p class="error">'+this.msg[this.lang][error]+'</p>';
}
$('div#msg').fadeOut('slow');
return this;
},
messageFields:function(message){
if(message){
$('form#form_sub').fadeOut();
$('div#msg').fadeIn();
this.$('msg').innerHTML = message;
$('div#msg').fadeOut(2000);
$('form#form_sub').fadeIn(2000);
}
},
menssageRegister:function(){
var register = 'register';
$('div#msg').fadeIn('slow');
this.$('msg').innerHTML = '<p class="success">'+this.msg[this.lang][register]+'</p>';
$('div#msg').fadeOut('slow');
return this;
},
$:function(element){
return document.getElementById(element);
},
fadeIn:function(element, time) {
target = this.$(element);
alpha = 0;
timer = (time*1000)/50;
var i = setInterval(
function() {
if (alpha >= 100)
clearInterval(i);
Newsletter.setAlpha(target, alpha);
alpha += 2;
}, timer);
},
fadeOut:function(element, time) {
target = this.$(element);
alpha = 100;
timer = (time*1000)/50;
var i = setInterval(
function() {
if (alpha <= 0)
clearInterval(i);
Newsletter.setAlpha(target, alpha);
alpha -= 2;
if(alpha===0){
//target.parentNode.removeChild(target);
}
}, timer);
},
setAlpha:function(target, alpha) {
target.style.filter = "alpha(opacity="+alpha+")";
target.style.opacity = alpha/100;
},
register:function(){
if(this.checkFields()){
this.registerEmail();
}
}
};
/**
* HTML
*
<style>
.newsletter #msg { background-color: #FAF9CF;border: 1px solid #CCCB96;color: #881A00;display: none; float: left;font-size: 14px; margin: 10px;padding: 10px;width: 95%; margin-top: -98px; min-height: 90px; margin-left:0px;}
.newsletter #msg .success { background-color: #FAF9CF; color: #079800; padding: 10px;}
.newsletter #msg .error { background-color: #FAF9CF; color: #881A00; padding: 10px;}
</style>
<div class="newsletter">
<form method="post" class="form_sub" id="form_sub">
<input type="text" name="nome" id="nome" class="nome" placeholder="Nome">
<input type="text" name="email" id="email" class="email" placeholder="Email">
<input type="button" class="submit" value="">
</form>
<div id="msg">&nbsp;</div>
</div>
*
*/
$(document).ready(function(){
$('div.newsletter > form#form_sub > input.submit').click(function(){
var name = $('div.newsletter > form#form_sub > input[name=nome]').val();
var email = $('div.newsletter > form#form_sub > input[name=email]').val();
//var lang = $(this).val();
Newsletter.name = name;
Newsletter.email = email;
//Newsletter.lang = lang;
Newsletter.register();
});
});
/**
* newsletter.php
*
<?php
header("Content-Type: application/json; charset=ISO-8859-1",true);
require("../conexao.php");
if($_REQUEST['action']=='cadastro'){
$error['email'] = $_REQUEST['email'];
$error['nome'] = $_REQUEST['nome'];
$query = mysql_query("SELECT nome,email FROM newsletter WHERE newsletter.email = '".mysql_real_escape_string($_REQUEST['email'])."';");
if(mysql_affected_rows()>0){
$error['error'] = true;
$error['cadastro'] = true;
}else{
$query = mysql_query("INSERT INTO newsletter (nome,email,data) VALUE('".mysql_real_escape_string($_REQUEST['nome'])."','".mysql_real_escape_string($_REQUEST['email'])."',NOW());");
if($query){
$error['error'] = false;
$error['cadastro'] = false;
}
}
}else{
$error['error'] = true;
}
//var_dump($error);
echo json_encode($error);
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment