Question

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.

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

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

My question is:

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 could help?

What do you think?

Regards

Update

As Borislav Sabev pointed, I do this:

Create a file upload using using: jQuery Form Plugin

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.

Then the JSON data is sent for processing via ajax POST

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

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
    });
}

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');
        }
    })
}

Now the big question:

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?

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>');
}
});
Was it helpful?

Solution

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

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.

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)

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

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

  • 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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top