使用Post/blockUI/ajaxform提交Ajax表单

Ajax Submit Form using Post/blockUI/ajaxform

本文关键字:Ajax 表单 提交 blockUI Post 使用 ajaxform      更新时间:2023-09-26

我有一个表单(三组复选框),用户可以在其中选择设备,然后选择在设备上运行的命令。在检查了用户想要运行的设备和命令后,他们会单击"继续"按钮。

现在,我只需要通过POST将表单数据发送到另一个PHP页面,在那里对信息进行解码。然后,从DB中提取设备的信息,并将其作为参数插入到使用PHPs EXEC命令运行的TCL脚本中。脚本大约需要15秒才能返回。

但是,我不想加载另一个页面,而是想使用JS$.blockUI()阻止该页面,提交表单,等待脚本返回,然后显示表单之前所在的返回内容。然后,很明显,取消阻止UI。

我的项目使用Zend框架。我有以下内容:

表格申报:

<form name="runcommands" action="/commands/execute/" method="post">

三个不同的复选框组(这是一个动态生成的表单):

"<input type='checkbox' name='globalcommand.$id' value='$id' />$command<br />";
"<input type='checkbox' name='projectcommand.$id' value='$id' />$command<br />";
"<input type='checkbox' name='device.$id' value='$id' />$hostname<br />";

我的javascript/ajax知识非常非常有限。这是我在JS中做的第一件事。该网站的其余部分几乎都是纯PHP/HTML。以下是我为JS所做的尝试。显然,它不起作用。

<script type="text/javascript">
        // Globals
        // prepare the form when the DOM is ready 
        var formd = ""; 
        $(document).ready(function() { 
            //$('#messageCenter').hide();
            //Form Ajax
            var options = { 
                beforeSubmit: beforeSubmit,  // pre-submit callback 
                success: showResponse  // post-submit callback 
            }; 
            $('#runcommands').ajaxForm(options); // bind form using 'ajaxForm' 
            $.ajax({
                type: "post",
                url: "/commands/execute/",
                data: {
                    formd: formd,
                    serverResponse: data.message
                },
                complete: finishAjax
            });
            $.unblockUI();
        }); 
        function finishAjax (data) {
            var ret = data.responseText;
            alert(ret);
        }
        function beforeSubmit (formData, jqForm, options) { 
            formd = $.param(formData); 
            $.blockUI();
            return true; 
        } 
        function showResponse(responseText, statusText, xhr, $form)  { 
            ret = responseText;
            alert(ret);
        } 
    </script>

在我的execute PHP页面中,我只是暂时回显脚本的输出。换一种方式做会更好吗?

感谢任何人的意见。我被困了,不知道从这里到哪里去。

Kevin

两种解决方案:

解决方案一:
目前,取消阻止被称为Ajax请求的启动,但您想做的是在返回响应后执行。将解锁添加到完整功能:

<script type="text/javascript">
    // Globals
    // prepare the form when the DOM is ready 
    var formd = ""; 
    $(document).ready(function() { 
        //$('#messageCenter').hide();
        //Form Ajax
        var options = { 
            beforeSubmit: beforeSubmit,  // pre-submit callback 
            success: showResponse  // post-submit callback 
        }; 
        $('#runcommands').ajaxForm(options); // bind form using 'ajaxForm' 
        $.ajax({
            type: "post",
            url: "/commands/execute/",
            data: {
                formd: formd,
                serverResponse: data.message
            },
            complete: finishAjax
        });
    }); 
    function finishAjax (data) {
        var ret = data.responseText;
        alert(ret);
        $.unblockUI();
    }
    function beforeSubmit (formData, jqForm, options) { 
        formd = $.param(formData); 
        $.blockUI();
        return true; 
    } 
    function showResponse(responseText, statusText, xhr, $form)  { 
        ret = responseText;
        alert(ret);
    } 
</script>

解决方案2:
通过将async选项设置为false来使ajax同步。这将阻止浏览器:

 $.ajax({
        type: "post",
        url: "/commands/execute/",
        async: false,
        data: {
            formd: formd,
            serverResponse: data.message
        },
        complete: finishAjax
    });