T
T
TechNOIR2018-04-18 10:11:59
PHP
TechNOIR, 2018-04-18 10:11:59

PHP. How to implement waiting for the execution of a function?

Good afternoon. Can you please tell me how to implement the waiting for the execution of the function?
There is a code:

here is the code
<?php
require 'ex/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;
$i = 2;
do {
$url1 = 'https://img1.goodfon.com/original/1920x1080/b/c9/pole-nebo-gorizont-peyzazh.jpg';
$url2 = 'https://get.wallhere.com/photo/1920x1200-px-nature-1054070.jpg';
$url3 = 'https://cdn.wallpapersafari.com/70/76/AnNVBR.jpg';


if (isset($_GET['test'])) {
    echo 'Вы нажали на кнопку: ' . $_GET['test'];
    $cell = 'F'.$i;
  $url = $_GET['test'];

  $inputFileName = './ex/e.xlsx';
  $spreadsheet = IOFactory::load($inputFileName);
  $spreadsheet->getActiveSheet()->setCellValue($cell, $url);


  $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
  $writer->save("./ex/e.xlsx");

  echo "Я внёс!";
  $i=$i+1;
}

  echo <<<HTML
<html>
<head>
<style>
img {
    height: 100px;
    width: 100px;
}
</style>
<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
</head>
<button onclick="set_mode_view(this)"><img src="$url1" alt="Кнопка «button»"></button>
<button onclick="set_mode_view(this)"><img src="$url2" alt="Кнопка «button»"></button>
<button onclick="set_mode_view(this)"><img src="$url3" alt="Кнопка «button»"></button>
<br/><br/>
<div id="test_value"></div>
<script>
function set_mode_view(element)
{
    $.ajax({
        url:'test4.php',
        data:{test:element.firstChild.currentSrc},
        success:function(data){
            $('#test_value').text(data);
        }
    });
  $(".current-msg").html("");
}
</script>

</html>

HTML;

} while ($i < 10);
?>


The HTML code draws 3 pictures. I click on one of them and it is entered into an excel spreadsheet.
If without a cycle, one-time execution, then everything is OK. But for further development, you need to add a cycle.
And then the problems began. Depending on where I put $i=$i+1; the code sometimes draws $i the number of times the html code, sometimes it goes in cycles.
Is it possible to somehow make the html code output, wait for the click, respectively, and only then issue the html code again. That is, pictures appear, click, data is entered and the html code appears again and until the cycle ends accordingly ..
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GTRxShock, 2018-04-18
@GTRxShock

https://developer.mozilla.org/en-US/docs/Web/Guide...

L
Lion Golden, 2018-04-18
@nimayoleynik

It is necessary to clearly separate the client and server parts. "Cycle" should be on the client ... on the server is also possible, but it's a crutch.
The server should accept requests - empty, with the selected image and position id.
With an empty request, it issues a client shell with scripts and pictures (the position is taken by default as 1, those pictures from 1 to 3 are given), when requested with arguments, it makes changes to the exel file and issues the following pictures, taking into account the current position, if there are no more pictures, it issues your text that there are no more pictures.
The client side should contain a div for images (by default, images will be loaded using 1 position, on the server side), js variable of the current position (by default 1, set on the server side when sending the client shell), jq image selection event and ajax function for processing it (when choosing ajax sends the selected image and the current position, in return it receives the images that need to be written to the div for images with content replacement).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question