Диалог закрывается очень быстро - PullRequest
0 голосов
/ 23 января 2019

У меня есть HTML-форма, которая загружается во всплывающем диалоговом окне.Когда я нажимаю кнопку формы, он отправляет данные в PHP, используя Ajax, и возвращает ответ PHP, который он показывает на метке.Проблема в том, что нажатие кнопки закрывает диалоговое окно так быстро, что оно не позволяет мне увидеть, что PHP ответил.

Я попытался поставить предупреждение, но оно появляется до того, как я улавливаю ответ PHP.

Какое решение я могу дать?

<div id="dialog" hidden="true" title="GENERAR CUOTAS">
    <div class="group">
        <form id="formulario" name="formulario">
            <table>
                <thead>
                    <tr style="height: 35px;">
                        <th colspan="2"><label><span>Elige el mes y el año</span></label></th>
                    </tr>   
                </thead>
                <tbody>
                    <tr>
                        <td><label for="Mes">Mes <span><em>(requerido)</em></span></label></td>
                        <td><?php include("../recursos/mesSelect.php"); ?></td>
                    </tr>
                    <tr>
                        <td><label for="anno">Año <span><em>(requerido)</em></span></label></td>
                        <td><input type="number" name="anno" min="1990" max="2500" size="4" value="<?php echo date("Y"); ?>" required/></td>
                    </tr>
                    <tr><td colspan="2"><button align="middle" class="formButtom" name="generar" onClick="genera();">Generar</button></td></tr>
                    <br><br>
                    <div id="status"></div>
                </tbody>
            </table>
        </form>
    </div>
</div>

Функция

   function genera(){
            // Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
            // Create some variables we need to send to our PHP file
            var url = "generaCuotas.php";

            var nMes = window.formulario.mes.value;
            var nAnno = window.formulario.anno.value;

            var vars = "mes="+nMes+"&anno="+nAnno;
            hr.open("POST", url, true);
            // Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            // Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if(hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    document.getElementById("status").innerHTML = return_data;
                }
            }
            // Send the data to PHP now... and wait for response to update the status div
            hr.send(vars); // Actually execute the request
            document.getElementById("status").innerHTML = "processing...";
    }

generaCuotas.php

<?php
include "../class/tCuotas.php";

    $mes=$_POST['mes'];
    $anno=$_POST['anno'];

    if ($mes <> '' && $anno <> '') {
        $oCuota = new tCuotas(1, 50, 1, 1);
        $respuesta = $oCuota->generaCuotas($mes, $anno);

        if($respuesta)
        {
            echo("<br>Cuota modificado correctamente");
        }
        else
        {
            echo("<br>Se ha producido un error al modificar el Cuota");
        }
    }
?>
...