E
E
Evgeniy2022-03-11 17:48:15
AJAX
Evgeniy, 2022-03-11 17:48:15

How to get code while running PHP on the front?

It is necessary to solve the problem associated with the Progressbar. How to get any text or numbers during PHP execution, for example, percentages separately until success.
$.ajax() and $.post() only return data at the end. $.ajax() has an xhr method. This method has progress, but how to get data at the time of pending I don’t understand how to get it.
That is, I need to get echo at the time of echo and not when everything is formed. How can I do that?
There is an idea, after all the checks, return AJAX and turn on the progressbar. Send a new AJAX to get the partial result and return again. And so send receive up to 100%. But that's just some kind of violence comes out over the server in my opinion.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2022-03-11
@Evdokim001

Yes, full of instructions https://yandex.ru/search/?text=ajax+progress&lr=21...

D
Dmitry Derepko, 2022-03-12
@xEpozZ

That is, I need to get echo at the time of echo and not when everything is formed. How can I do that?

When and what to get?
----------
You would tell what you are doing on the server, and from this you can already draw conclusions in favor of this or that solution.
To be honest, I did not understand anything that you wrote in the description and I will answer the question " How to make a progress bar for a long process in PHP ":
To simplify my examples, let's talk about the food delivery process.
We will make 4 stages in this process:
1. Send an order and receive confirmation from the service provider - a restaurant that will prepare rolls and pizza for us in the evening, but without a dry crust.
2. Find a courier and send him to the restaurant to wait for the order.
3. Wait until the restaurant prepares the order.
4. Deliver the order to a hungry customer.
-----
You need storage for logging process steps: SQL, NoSQL, file or other storages.
I advise you to highlight several properties of the process:
1. Stage of execution. We will call it State in the future.
2. General progress. Let's call it Total progress .
Then you can stumble a little and create a logging of the execution of each of the stages. But already without me ;)
After receiving a request to execute your complex process, you need to create a new entry in the repository and give the ID of this entry in response to the request. On the frontend, in your case.
The next step is to connect the logging of state transitions to the places where they start. The code will be something like this:
$state->setState("first state");
$storage->save($state);
// Отправить запрос на готовку заказа
// ...
// Ответ положительный

$state->setState("second state");
$storage->save($state);
// Найти курьера
// ...
// Курьер найден

$state->setState("first state");
$storage->save($state);

The stages are being logged, now you need to show the process changes on the frontend.
We create a new endpoint, which will:
1. Accept the process id that we gave to the front when creating the process
2. Go to the repository and see the current stage and how many there are in total. In our example, there are only 4 of them.
3. Do the calculation you need. For example, say "we are now at 2 of 4 stages" or 2 divided by 4 and say "50% of the process is completed". As you like it, do it.
----------------
I think I answered my question well, and even gave you some ideas :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question