使用PHP,jQuery和Ajax进行交互式文件处理 [英] Interactive file processing in using PHP, jQuery and Ajax

查看:106
本文介绍了使用PHP,jQuery和Ajax进行交互式文件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将旧的MS-DOS Foxpro程序迁移到cakePHP,但是在应用程序过程的某些部分中,用户将可以加载文本文件并根据某些业务逻辑对其进行处理.

I'm migrating an old MS-DOS Foxpro program to cakePHP, but in some part of the application process the user will have the possibility to load a text file and process it according to some business logic.

我使用了该算法,并且运行良好,我用普通的PHP对其进行了编程.

I worked the algorithm, and it's working fine, I programmed it in plain PHP.

但是在过程的某些部分中,用户必须交互执行某些任务,在纯PHP中是可以做到的.

But in some parts of the process the user must interact performing some tasks, in plain PHP it's possible to do it.

我的问题是:

我如何在MVC框架上进行交互式文件处理,因为当控制器将数据交给视图时,控制器已经处理了数据.

How can I have an interactive file process on a MVC framework, since the controller had processed the data when it`s give the control to the view.?

jQuery-ajax可以帮助您吗?

jQuery-ajax could help?

您怎么看?

致谢

更新

正如Borislav Sabev所指出的,我这样做:

As Borislav Sabev pointed, I do this:

使用以下文件创建文件上传: jQuery表单插件

Create a file upload using using: jQuery Form Plugin

根据上传文件上的数据结构解析的PHP文件,该文件将以JSON格式解析的内容返回给客户端.

A PHP file parsing according the data structure on the uploaded file that returns to the client the parsed contents of the file in JSON.

然后通过ajax POST发送JSON数据进行处理

Then the JSON data is sent for processing via ajax POST

$.each(data, function (i, fb) {
    callAJAX(fb);
    });

callAJAX只是发送POST请求

callAJAX just send the POST request

function callAJAX(fb){
    $.ajax({
        type: 'POST',
        url: "proc.php",
        dataType:"json",
        complete: function(r,st){
        },
        success: function(r,st){
            processError(r);
            })
        },
        async: false
    });
}

如果存在翻译错误,PHP将通过JSON向客户端返回错误消息.

If there is a translation error, the PHP returns to the client an error message via JSON.

function proccessError(r)
{
    $.each(r,function(i,d){
        if (d['error'] == 1){
            $.alert('Material not found in translation table','Error');
        }
    })
}

现在是一个大问题:

当我打开$ .alert对话框时,我希望用户通过自动完成功能选择正确的材料,但是代码没有等待用户选择,而是弹出了另一个$ .alert窗口.如何告诉jquery等待用户输入然后继续处理?

When I open the $.alert dialog I want the user select the correct material via an autocomplete, but the code is no waiting for the user selection and pops another $.alert window. How can I tell jquery to wait to the user input an then continue with the processing?

顺便说一句:这是我使用的$ .alert扩展名:

BTW: this is the $.alert extension that I use:

$.extend({ alert: function (message, title) {
  $("<div></div>").dialog( {
    buttons: { "Ok": function () { $(this).dialog("close"); } },
    close: function (event, ui) { $(this).remove(); },
    resizable: false,
    title: title,
    modal: true
  }).text(message)
  .parents(".ui-dialog:eq(0)").wrap('<div class="error-w"></div>');
}
});

推荐答案

@ AD7six告诉您的内容是完全正确的,但是您的问题非常模糊.

What @AD7six told you is completely right however your question is very vague.

您似乎需要提供一种交互式(实时?)方式,供用户在文件上传和处理后编辑数据?.如果它是按行排列的数据",我可以建议使用一些DataGrid-将其想象为文件结构的Web表示.

What you seem to need is to provide an interactive (realtime?) way for a user to edit data after a files has been uploaded and processed?. If it is 'data arranged in rows' I can suggest some DataGrid - imagine this as a Web representation of your file's structure.

您要用视图显示控制器上的方法或操作的结果"来说的是,这种情况在普通的HTTP(使用MVC)流中:

What you're trying to say with 'the view shows the result of a method or action on the controller' is that this case is in the normal HTTP (with MVC) flow:

浏览器(用户)发起请求 => 我的控制器已处理请求 => 调用了模型(获取,保存或处理数据) => 数据返回给控制器 => 控制器将数据处理到视图层 => 视图层呈现,控制器返回响应(呈现的视图)

Browser (user) initiates a request => Request is processed my Controller => Model is called (gets, saves or manipulates data) => Data is returned to Controller => Controller handles the data over to the View layer => The view layer renders, Controller returns the Response (the rendered view)

为了交互地执行此操作,您将必须使用 AJAX (我建议 AJAJ ).

In order to do this thing interactively you will have to use AJAX (I propose AJAJ).

在这种情况下的总体范例是,对每个AJAX请求都重复执行上述流程.您可以通过多种方式实现这一目标:

The overall paradigm in this case is that the above flow is iterated on every AJAX request. You can achieve this in many ways:

  • 使用普通的JavaScript和 XMLHttpRequest
  • 使用jQuery的XMLHttpRequest包装函数($.ajax()$.post()$.get()$.load()等)
  • 其他JS库的AJAX功能(例如, MooTools )
  • 使用实时 JS应用框架
  • Use plain JavaScript and XMLHttpRequest
  • Use jQuery's XMLHttpRequest wrapper functions ($.ajax(), $.post(), $.get(), $.load(), etc.)
  • Some other JS lib's AJAX capabilities (MooTools for example)
  • Use an real-time JS app framework

这在很大程度上还取决于您需要支持的浏览器(和版本). 您将需要通过控制器或完整的API为客户端提供访问"权限. API的最佳解决方案是 REST .如果这是企业应用程序,建议您遵循理查森成熟度模型.

This also heavily depends on the browsers (and versions) you need to support. You will need to provide 'access' for the client-side via a controller or a full API. The best solution for an API would be REST. If this is an enterprise app I suggest you adhere to the Richardson Maturity Model.

如果您不需要完全实时的应用程序,则可以使用jQuery AJAX或其他JS lib的AJAX. 问题是您要求作为回答是一个整体的解释,这就是为什么我不提供代码示例. Cake具有 JSON视图,您可以使用它返回数据. 另一个选项是空白布局和返回JSON编码数据的视图,例如:

If you don't need a fully-realtime app I'd go with jQuery AJAX or some other JS lib's AJAX. The thing is that what you request as an answer is an overall explanation and that is why I am not going to provide code examples. Cake has JSON views that you can use to return data. The other option is an empty layout and a view that returns JSON encoded data like:

<?php echo json_encode($data);?>

希望这会有所帮助.如果有什么问题.

Hope this helps. If anything ask.

这篇关于使用PHP,jQuery和Ajax进行交互式文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆