对话框关闭非常快

时间:2019-01-23 11:35:57

标签: javascript php ajax dialog popup

我有一个在弹出对话框中加载的HTML表单。当我按下表单按钮时,它使用Ajax将数据发送到PHP,并返回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");
        }
    }
?>

0 个答案:

没有答案