$_POST without closing modal bootstrap

Asked

Viewed 546 times

1

I have to open a form in a modal using bootstrap and when sending it I want to receive the data without closing the modal

I open the modal like this:

<a data-toggle="modal" data-target="#modalEMP" href="consulta.php">Consultar CNPJ</a>

The.php query file has the following form:

<form id="formulario" action="resultado.php" method="post">
    <input type="text" name="CNPJ" id="CNPJ" maxlength="19" required />
    <input id="submit" name="enviar" type="submit" value="Consultar" />
</form>

This way when sending the form the modal closes automatically. I want the post to be sent and show me the page php result. in the same modal.

1 answer

1

With this way of sending, the page refreshes completely and returns the modal to initial pattern (which is Hidden). Use jquery and ajax to give an Submit in the form.

<form id="formulario" action="resultado.php" method="post">
   <input type="text" name="CNPJ" id="CNPJ" maxlength="19" required />
    <input id="submit" name="enviar" type="submit" value="Consultar" />
</form>

<script type="text/javascript">
    var frm = $('#formulario');
    frm.submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            success: function (data) {
                alert('ok');
            }
        });

    });
</script>

Browser other questions tagged

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