$ _POST without closing modal bootstrap

1

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

I open the modal like this:

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

The query.php 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>

In this way, when sending the form, the modal closes automatically. I want the post to be sent and show me the resultado.php page in the same modal.

    
asked by anonymous 22.02.2016 / 01:40

1 answer

1

With this form of submitting, the page refreshes completely and returns the modal to the initial default (which is hidden). Use jquery and ajax to give a submit in 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>
    
13.05.2016 / 18:18