PHP generate confirmation Pop-Up

Asked

Viewed 1,301 times

2

I have a php code that makes insertions in the database, data that came through forms, how is it possible to return a message through a pop-up to the user informing him if there was success or not his registration/query? and without there being a redirect on the screen?

  • I find it more that it lacks clarity than the question is too broad. Anyway, it depends on the author’s edits, complementing the content, so it can be reopened.

  • PHP, when used to display web pages, does not popup, because it runs on the server and not on the client. It would have to add JS code for this.

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 (upload) 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 really looking for is the AJAX part in the javascript code, you can deal with how the information will be returned in other ways (using the function .html() for example).

Browser other questions tagged

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