1
I would like to stylize the return messages of a Newsletter that I have on my site, the attempts I made did not give the expected result and I believe that the lack of knowledge in the subject has hindered much.
What I got today is this:
<script type="text/javascript">
$(document).ready(function(){
$("#newsletter-form").submit(function(){
var valor = $("input[name=newsletter]").val();
if (valor != "" ) {
$.ajax({
type: "POST",
url: "newsletter.php",
data: $("#newsletter").serialize(),
dataType: "json",
success: function(msg){
$("#Resposta").removeClass('sucesso');
$("#Resposta").addClass(msg.status);
$("#Resposta").html(msg.message);
},
error: function(){
$("#Resposta").removeClass('erro');
$("#Resposta").addClass('erro');
$("#Resposta").html(msg.message);
}
});
return false;
}
});
});
The message of success in a div
green and error in a red and that both disappear after a period, I have managed to do this, but I do not know if in a correct way.
The definition of the messages are like this in my .php
:
<?php
require_once('Connections/conexao.php');
// Recebendo o array com os ID´S
$email = $_POST['newsletter'];
//response array with status code and message
$response_array = array();
// Realiza verificação se já existe e-mail gravado no banco
mysql_select_db($database_conexao, $conexao);
$sql = "SELECT email FROM newsletter WHERE email = '$email'";
$busca = mysql_query($sql,$conexao);
$linhas = mysql_num_rows($busca);
if ( $linhas > 0 ){
//set the response
$response_array['status'] = 'erro';
$response_array['message'] = '<p style="color:#f25824">E-mail já cadastrado em nossa base</p>';
} else {
$sql = "INSERT INTO newsletter (email, status ) VALUES ('$email', 1)";
mysql_select_db($database_conexao, $conexao);
$sql = mysql_query($sql);
if ($sql) {
$response_array['status'] = 'sucesso';
$response_array['message'] = '<p style="color:#669900">Você se inscreveu com sucesso para a nossa newsletter.</p>';
} else {
$response_array['status'] = 'erro';
$response_array['message'] = '<p style="color:#f25824">O seu endereço de email não pode ser subscrito porque ocorreu um erro no servidor. Por favor, tente novamente mais tarde.</p>';
}
}
echo json_encode($response_array);
?>
The result of my last attempt can be seen here, when registering an email for newsletter:
I use a quick notification ready library called Toastr by Codeseven, which I recommend and would serve exactly for your purpose. [http://codeseven.github.io/toastr/] I’m going to put an example code as a response to how I use it.
– Rafael Withoeft
Hello @Rafael Withoeft, thanks for the tip but the page is in error.
– adventistapr
Sorry, here clicked normally... anyway I put an example at your disposal if you find interesting.
– Rafael Withoeft