简体   繁体   English

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

[英]Ajax Submit Form using Post/blockUI/ajaxform

I have a form (three groups of checkboxes) where users can choose devices and then choose commands to run on them. 我有一个表单(三组复选框),用户可以在其中选择设备,然后选择要在它们上运行的命令。 After checking which devices and commands the user wants to run, they click a continue button. 在检查了用户要运行的设备和命令后,他们单击继续按钮。

Right now, I just have the form data being sent via POST to another PHP page where the info is decoded. 现在,我只是将表单数据通过POST发送到另一个PHP页面,在该页面中对信息进行解码。 Then, the device's information is pulled from a DB and is inserted as parameters into a TCL script which is run using PHPs EXEC command. 然后,将设备的信息从数据库中提取出来,并作为参数插入到使用PHP的EXEC命令运行的TCL脚本中。 The script takes ~15 seconds to return. 该脚本大约需要15秒才能返回。

However, instead of having to load another page, I would like to block the page using JS $.blockUI(), submit the form, wait for the script to return, and then display the returned content where the form was previously located. 但是,我不必加载另一个页面,而是想使用JS $ .blockUI()阻止该页面,提交表单,等待脚本返回,然后显示返回的内容先前位于表单的位置。 Then, obviously unblock the UI. 然后,显然要取消阻止UI。

I am using Zend Framework for my project. 我正在为我的项目使用Zend Framework。 I have the following: 我有以下几点:

Form declaration: 表格声明:

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

Three different check box groups (this is a dynamically generated form): 三个不同的复选框组(这是动态生成的表单):

"<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 />";

My javascript/ajax knowledge is VERY VERY VERY limited. 我的JavaScript / ajax知识非常有限。 This is the first thing I've ever done in JS. 这是我在JS中做过的第一件事。 The rest of the site is pretty much pure PHP/HTML. 该网站的其余部分几乎都是纯PHP / HTML。 Here is what I've attempted for the JS. 这是我为JS所做的尝试。 Obviously, it doesn't work. 显然,它不起作用。

<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>

In my execute PHP page, I just echo out the script's output for right now. 在我的执行PHP页面中,我现在只是回显脚本的输出。 Would it be better to do that another way as well? 更好地采用另一种方式吗?

Thank anyone for any input. 感谢任何人的投入。 I am stuck and have no idea where to go from here. 我被困住了,不知道从这里去哪里。

Kevin 凯文

Two solutions: 两种解决方案:

Solution one: 解决方法一:
Currently the unblock is called the initiation of the Ajax request, but what you want to do is do after the response is returned. 当前,取消阻止被称为Ajax请求的启动,但是您要做的是在返回响应之后执行。 Add the unblock to the complete function: 将取消阻止添加到完整功能中:

<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>

Solution 2: 解决方案2:
Make the ajax synchronous by setting the async option to false. 通过将async选项设置为false使ajax同步。 This will block the browser: 这将阻止浏览器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM