PHP Generate Popup Confirmation [Closed]

2

I have a php code that makes inserts into the database, these data came through forms, how can you return a message through a popup to the user informing him whether or not his registration / query succeeded ? and there is no redirection on the screen?

    
asked by anonymous 09.01.2015 / 17:03

1 answer

3

You can do this using AJAX in a simple way.

Considering the following form:

<form id="form-register">
    <label for="inp-nome">Nome:</label>
    <input type="text" id="inp-nome" />

    <button type="submit">Enviar</button>
</form>

We treat your submit event with the following JQuery code:

$('#form-register').submit(function(e) {
    e.preventDefault();

    var nome = $('#inp-nome').val();
    var postForm = {
        'nome': nome
    };

    $.ajax({
        type: 'POST', // Usa o método POST
        url: 'pagina.php', // Página que será enviada o formulário
        data: postForm, // Conteúdo do envio
        success: function(data) {
            if (data == 'sucesso') {
                alert('Foo');
            } else {
                alert('Bar');
            }
        }
    });
});

Here is the PHP code that will insert into the database:

<?php
    $nome = $_POST['nome'];

    // Código de tratamentos/inserções no banco aqui


    // Caso tenha inserido com sucesso
    echo "sucesso";
    // Caso contrário
    echo "falha";
?>

Note that what you are looking for is the part of AJAX in the javascript code, you can see how the information will be returned in other ways (using .html() function).

    
09.01.2015 / 17:15