Good day to all!
The question is: there is an html page. There is a form for sending data on it, loading and uploading a csv file and loading the archive.
The csv stores email addresses. Archive - attachment to the letter.
The form is processed by the jQuery script (jQuery Form library):

$(document).ready(function(){ $("form").submit(function(){ var options = { url: 'sent.php', dataType: 'html', status: $(".result").html('<img src="spinner.gif" width="16" height="16" />'), type: 'POST', success: function(msg){ $(".result").append(msg); } }; $(this).ajaxSubmit(options); return false; }) }); 

I really want PHP to give, and JS to process what is given, like: "File uploaded", "sending to% email_address% ... OK!" etc.
But I understand that JS responds to the server response.
How to make it dynamic and dynamically displayed data?

  • 3
    Comet? Googling, maybe this is what you want. - rnd_d
  • and this is interesting. I will not close the question yet. Let's see what we come to, maybe we will think of something new. Thanks for the advice. Delved into reading ... - kemerov4anin
  • there is no reason to even use nodejs for such a small application. We are looking for other options - kemerov4anin

5 answers 5

  1. Use sockets (two-way communication). Then the server when performing a certain stage can easily inform the client about it.

  2. Long polling ( php example ). The essence of the method is simple. We send a request to the server, if nothing has been processed yet, the server simply does not return a response (keeps the connection open - in php it can be implemented using sleep ). As soon as something happens, the server closes the connection, giving you the data you need in the response (successfully processed stage in your case). As soon as the client receives an answer, he forwards the request, waiting for the next answer.

But here, yes, when a connection is rediscovered, there are no guarantees that several stages are not completed during this time - therefore, it is better to keep a log of not sent statuses.

    Immediately tell your opinion - it's not worth it. You will spend a lot of time, will encounter browser incompatibility and a bunch of bugs. I advise you to implement it only if it is a kind of test project and you do it for your own experience (there are no limitations in time and resources :)


    Well, now let's go to practice:

    In the classical approach, the Server can only respond to requests from Clients (browsers) and nothing else. The server itself cannot initiate a request for clients. Get over it.

    As mentioned above in the comments - read about Comet. Excellent article on Habré . Here are the main approaches to solve this problem.

    In the case of PHP, take a look at PHP WebSocket

    The standard example in this case is the simplest chat. It's like Hello, world! when learning a new language. To alter such an example to fit your needs is not at all difficult. The main thing to grasp the essence and pull out a piece of code that is responsible for the response from the server to the client. There it is already possible to transfer anything and handle accordingly scripts on the client.

    At one time, when my hosting did not allow to implement anything like this, I found an implementation, when connecting to each client, a unique file is created on the hosting, in which all the info is written, and each client already reads the information from its file. In general, you can talk here for a long time and a lot. If you were looking for a standard solution in one line, then it is not ...

      It is necessary to use Ajax requests, here is a general scheme, without details:

      1. first hang the event on the form submit button
      2. in the event we make an AJAX request to the server, in it in the beforeSend branch we turn on the display “data is sent”, in success (the request is successful) we cut off the display “data is sent” and output “data is sent”; You can add something to the error (if an error occurred) - at will.
       $(document).ready(function(){ $("$submit-id").click(function(){ $.ajax({ //parameters beforeSend: function(){},//включили "данные отпраляются" success: function(){},//выключили и показали сообщение, что всё хорошо error: function(){alert("Ошибка!");} }); return false;//что бы форма не отправилась обычным способом }); }); 
      • It seems that you did not understand the essence of the question. We will wait for more answers or smoke manuals - kemerov4anin
      • Well, I realized that you want to visually display the sending, or not? Then specify :) - metazet 6:01 pm
      • I want, during the execution of the script, the responses from the server, such as echo ('everything is ok!'); podguzhalis in the body of the page. I see the opportunity to do this only if I force the php script to write "logs" from these responses, and ask another ajax request "what is written in the log". And output, say by setting setTimeout (check_log (), 1000); - kemerov4anin
      • setTimeout (check_log (), 1000); stack overflow. We smoke further - kemerov4anin

      Well, in general, this is not the right architecture in the framework of the web service (and in a close approach it is) to try to climb inside the web service method outside. The breakdown of ideology, in consequence of which all sorts of "hacks" are born.

      In my opinion the right decision here will be the following:

      1. Creaks (Web Service Method) does something, while he reports on the results of his work through some project API.

      2. The client initiating this process hangs on the js event of the Object that polls the server or with some frequency to find out what is reported (calls a script that requests through API and who reports what) and if it does, it calls subscribers the way it finds out (via flash sockets for example)

      In the project API, the implementation of methods - for example, stupidly through the base. In this case, the method from clause one can additionally initiate a request through a socket if the client supports it.

      Something like this :-)

      And if you actually want to see the console output of the script, then the long pull option and php disabling buffering will help you. those. determine iframe with your script and what it will throw because no buffer will be displayed in the browser. Only you need to give the text, because The browser can wait for the completion of tags for example for building and displaying the house model.

        Maybe in the script itself, enable output buffering, and on js catch responses from the server? as an option...

        • buffering in pkhp? What's the point? I now the only thing I’ve done is googling it through another frame in which I’ll read the log and load the java script into the result element of what php will produce. - kemerov4anin
        • As I understand it, when you run a script, do you perform an action, only after the completion of this action does the server send a response? - or_die
        • well, in general, ajax with the help of polling works this way, i.e. catches onreadystage = 4. And now everything is exactly like that - kemerov4anin 5:56 pm