How do I create a variable to display error or success message?

Asked

Viewed 503 times

3

How do I display error messages in a variable, for example: Executed the code all right then has to appear the first message:

        if($count==1)
        {
        // PRIMEIRA MENSAGEM

            $change_profile_msg[] = "<div class='alert alert-success'>
            <button type='button' class='close' data-dismiss='alert'>&times;</button>
            <strong>Sucesso!</strong> Perfil atualizado.
            </div>";            
        }
        else
        {
            $change_profile_msg[] = "<div class='alert alert-danger'>
            <button type='button' class='close' data-dismiss='alert'>&times;</button>
            <strong>Erro ao atualizar!</strong> Não foi possível atualizar o usuario.
            </div>";
        }

There in the place I want to display the message:

$main_content .= ' EXIBIR MENSAGEM AQUI DENTRO APOS TUDO OCORRER BEM OU MAL NO SUBMIT DO FORMULARIO ';

It would be something like:

foreach($change_profile_msgs $change_profile_msg) {
      $main_content .= ''.$change_profile_msg;
}

Only then it will display error message saying something like Undefined variable and will only show the message when running Submit, how do I hide that foreach until run the form and return something to display the message ?

  • You can check the type of request, if a GET user is opening the registration screen, if it is POST he is recording the information.

1 answer

1


Create a session.

Use the following javascript:

//Função responsável por exibir ou ocutar o feedback mensage
function showAlert(type, message) {
    if (message !== '') {
        if (type === '') {
            type = 'success';
        }
        $('#alert').removeClass();
        $('#alert').addClass('alert-' + type).html(message).slideDown();
        setTimeout("closeAlert()", 15000); // 15 segundos
    }
}

//chama a função para fechar o alerta quando clicado.
$(function () {
    $('#alert').click(function () {
        closeAlert();
    });
});
//
function closeAlert() {
    $('#alert').slideUp();
    $('#alert').removeClass();
}

On your php page at head where :

       <script>
            window.onload = function () {
                showAlert('Error', 'Bem vindo, visitante');
            };

        </script>

On your php page on body:

<div id="alert"></div>

THE CSS:

#alert {
    display: none;/* O alerta deve iniciar oculto */
    min-width: 300px;
    height: auto;
/*    border-radius:10px;*/
    font-family:Tahoma,Geneva,Arial,sans-serif;font-size:12px;
    font-weight: bold;
    overflow: hidden;
    /*position: fixed;*/
    position: absolute;
    left: 50%;
    margin-left: -160px;
    z-index: 999999;
    padding: 10px;
    color: #fff;
    cursor: pointer;
    top: 0;
    text-align: center;
    margin-top: 5px;
}

.alert-error {
    background:#FF5252;
    border:1px solid #f5aca6;

}

.alert-success {
    background: #0f9d58;
    border:1px solid #2ecc71;
}

Upshot:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.