keydown event in php?

1

this the context. I'm going to load a text file in php, and once loaded, I want to show on the screen a line of the text file every time I press the "s" key. I know javascript and I usually know how the keydown event works, but I get confused about the integration from the part of php that is responsible for processing the file. Someone to clarify how I can do it please. Thanks

My page would be like this. I would have an input file to select a text.txt file, a send button. Then the first line of the text file would be shown, then I would press the "s" key and it would show me the next line, and so on.

    
asked by yavg 10.07.2017 в 18:14
source

2 answers

-1

Inside the body, try this: Ajax is not necessary for this case, although the code is somewhat intricate.

<form action="index.php" method="post" enctype="multipart/form-data">
    <input type="file" name="archivo" />
    <br>
    <br>
    <input type="submit" name="submit" value="Enviar" />
</form>

     <?php
    $ultimo_archivo;

    if(isset($_POST['submit'])) {
        if(is_uploaded_file($_FILES['archivo']['tmp_name'])) {
            $ultimo_archivo = $_FILES['archivo']['name'];
            move_uploaded_file($_FILES['archivo']['tmp_name'], $ultimo_archivo);
            $lineas_archivo = file($ultimo_archivo);
                ?>
                    <script>
                        let cont = 0; // Pone el contador a 0;
                        const lineas = <?php echo json_encode($lineas_archivo); ?>;

                        <?php
                        foreach($lineas_archivo as $k => &$l) {
                            if($k != count($lineas_archivo)-1) {
                                $l = str_replace("\r\n", " S \r\n", $l);
                            }
                            else{                                   
                                $l = str_replace($l, $l." S", $l);
                            }
                        }

                        fopen($ultimo_archivo, "w+");
                        file_put_contents($ultimo_archivo, implode("", $lineas_archivo));

                        ?>

                        //Añade la primera linea
                        document.body.append(lineas[cont++]);

                        //Añade un evento cuando se pulse la letra S
                        addEventListener("keydown", function(event) {
                            if(event.key === "s" || event.key === "S") {
                                if(cont <= lineas.length-1) {
                                    //Agrega la siguiente linea e incrementa el contador

                                    //Modifica la linea y agrega una S al final
                                    linea_actual = lineas[cont++];
                                    document.body.innerHTML += "<br>";

                                    document.body.append(linea_actual);
                                }
                            }
                        });

                    </script>
                <?php
        }
    }
?>
    
answered by 10.07.2017 / 22:40
source
2

Logica: first of all, what you want to do is done in javascript or jquery , the most correct way is by ajax , in this case when you upload and process the file normally the php will have an answer of ok finish ... in that case what you can do is read the text file and print it with a echo , as the request was made medium ajax this will have a callback or data return from the php, it is called response good take the response and write it in a hidden textarea: <textarea name="hide" style="display:none;"></textarea> game of this you do a javascript that with keydown s navigate the textarea lines.

since there is no keydown on the php side, remember that php runs on the server side and the keydown event occurs on the client side. Greetings.

Requests:

working in this way will help you a lot since you only have to make a request by ajax the same where you upload the file does everything and the response is the result, this amounts to a single request.

then every time you press the 's' it will not make a request because the text is already in the textarea that you filled in the answer. and the route by line you can do with a function in jquey / javascript this does not generate requests to the server.

In this case I could not give you a code as an answer because you have not placed any progress of your code. When you have something more concrete I will check if I have time to give you a better answer. Greetings.

    
answered by 10.07.2017 в 19:08