A
A
Anton2021-07-01 01:12:32
PHP
Anton, 2021-07-01 01:12:32

How to check if string exists in txt via php and write it to txt file?

Please help me how to check for the presence of the innerhtml line in txt using php. If there is no line in txt, write it down, and if there is, display a notification that it already exists.
For example, there is an element containing some text in this case Sometext When the button is pressed , the innerhtml of the above element changes to any other, for example Sometext1 and is checked for presence in the txt file, if there is a notification output, if not, it writes to a new line in the txt file. UPD: In general, I wrote check and write in php, but how to make it all asynchronous, and how to pass the innerhtml value from the DOM to the $hex variable is not yet clear...
<td id="text">Sometext</td>
<button type="submit" id="btncheck">Check</button>
<td>

<td>0101010110101001010110</td> // как это передать в $hex?
<?php
function checkhex() {
// Читаем файл построчно
function getLines($file) {
    $f = fopen($file, 'r');
    try {
        while ($line = fgets($f)) {
            yield $line;
        }
    } finally {
        fclose($f);
    }
}

$file = "hex.txt";
$hex = "00000000000000000000000000000000000000000000000000000000000001"; // Как передать сюда из html?
$exists = false;

// Проверяем хекс
foreach (getLines($file) as $line) {
    $exists = $exists || trim($line) === $hex;
}

if ($exists) {
    echo "<font color='red;'>Имеется в базе</font><br>";
} else {
  echo "<font color='red;'>Не имеется в базе, записываем!</font><br>";
  $filed = "hex.txt";
    $rez = file_get_contents($filed);
    $rez .= "\r\n$hex";
    file_put_contents($filed, $rez);
}
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aricus, 2021-07-01
@Aricus

To pass data from frontend to backend use jquery + ajax. When preparing data for submission, you can extract a piece of html code from the page itself (if I understand correctly).
Then you can process this data in php on the server, including all manipulations with the txt file.
As a result of processing, display on the page (echo '';), the text that jquery will receive. Depending on it, display the appropriate message to the user.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question