【问题标题】:Ajax Submit Form using Post/blockUI/ajaxform使用 Post/blockUI/ajaxform 提交表单
【发布时间】:2012-03-14 20:44:20
【问题描述】:

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

现在,我只是将表单数据通过 POST 发送到另一个 PHP 页面,在该页面中对信息进行解码。然后,从数据库中提取设备信息并作为参数插入到 TCL 脚本中,该脚本使用 PHP 的 EXEC 命令运行。脚本需要大约 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>

在我的执行 PHP 页面中,我现在只是回显脚本的输出。换一种方式会更好吗?

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

凯文

【问题讨论】:

    标签: php javascript ajax zend-framework ajaxform


    【解决方案1】:

    两种解决方案:

    解决方案一:
    目前解除阻塞称为发起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
        });
    

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 2016-06-23
      • 2011-01-31
      • 1970-01-01
      相关资源
      最近更新 更多