How to implement the condition of checking the txt file (or another) is there an id in the file? If the user id is in the list on the server, then the function in the condition is executed, and if id is not in the list, a window is displayed.

I do not know how to implement a list with the necessary id. And how to take from there data for comparison.

  • Something I do not understand what the problem is. On the server you have a file. Send a request containing the id to the server, catch and check - UserName
  • And how to implement it programmatically? Theoretically, I know how it all works, by the request of ajax I send a request that contains an id, sort of like. And all that should be on the server itself, I don `t know how to do - SloGS
  • "And how to take from there data for comparison." then I confess I did not understand what it was about. Therefore, I described only the scheme about which I spoke above - UserName
  • @UserName take data from txt file, there is one big line with numbers separated by commas - SloGS

1 answer 1

It must be something like this:

On the server there should be a script that will receive the request and give the answer.

<?php header("Access-Control-Allow-Origin: *"); if ($_GET["id"]) { echo "ID" . $_GET["id"] . "присутствует в базе!"; } 

The above is a primitive example. Set the header Access-Control-Allow-Origin . Its installation means that the server allows a cross-domain request. If it does not exist, the browser will not receive it and will terminate the request with an error.

Next, we perform a primitive id check, which will be sent from the extension.

Checking the GET array. Yeah, id passed. Display a message. That is, the echo construction output will be transmitted as an answer.

In fact, you will have advanced processing. Ie, do not just look at the presence of GET["id"] , but check for the presence in the file and so on.

The extension should have something like:

 var xmlHttpRequest = (function () { var _xmlHttpRequest = new XMLHttpRequest(); function sendRequest(url) { return new Promise(function (resolve, reject) { _xmlHttpRequest.onload = function () { resolve(_xmlHttpRequest.responseText); }; _xmlHttpRequest.onerror = function () { reject(_xmlHttpRequest.statusText); }; _xmlHttpRequest.open("get", url, true); _xmlHttpRequest.send(); }); } return { sendRequest: sendRequest } })(); 

You can call this whole thing like this:

 xmlHttpRequest.sendRequest("http://localhost?id=1").then(function (result) { alert(result); }); 

Replace the sendRequest function sendRequest with your own. I checked on my computer, so I specified localhost .

Here we send a get request to the specified address and wait for the result of the execution. As soon as a response is received from the server, you will see a pop-up window.

References:

XMLHttpRequest Basics

XMLHttpRequest: cross-domain requests

JavaScript promises (this is about new Promise in the code above)

UPD

take data from txt file, there is one big line with digits separated by comma

Well, then do something like:

 if($_GET["id"]) { $data = file_get_contents("data.txt"); $data = explode(",", $data); if(array_search($_GET["id"],$data) !== false ) { echo "ID" . " " . $_GET["id"] . " " . "присутствует в файле"; } } 

See if there is an id . If there is, then read the file. Convert the contents of the file into an array ( explode converts a string into an array using the separator "," ).

Next, we check the presence of the incoming id in the array using the function array_search .

References:

array_search

explode

but this is what the browser console displays when promising {Promise {: "pending"}

If you read what I dropped you might have noticed:

A promise can be: fulfilled - successfully completed rejected - completed with a pending error - not completed settled - completed with any outcome

State diagram Image More about promise

  • Should the request be so? xmlHttpRequest.sendRequest("file:///C:/test.php?id=1").then(function (result) { alert(result); }); - SloGS
  • and I made the php file so, I understand very badly in php, I hope correctly <?php header("Access-Control-Allow-Origin: *"); $file = file_get_contents('2.txt'); if (preg_match($_GET["id"], $file) { echo "ID" . $_GET["id"] . "присутствует в базе!"; } ?> <?php header("Access-Control-Allow-Origin: *"); $file = file_get_contents('2.txt'); if (preg_match($_GET["id"], $file) { echo "ID" . $_GET["id"] . "присутствует в базе!"; } ?> <?php header("Access-Control-Allow-Origin: *"); $file = file_get_contents('2.txt'); if (preg_match($_GET["id"], $file) { echo "ID" . $_GET["id"] . "присутствует в базе!"; } ?> - SloGS
  • but what the browser console displays when prompted by Promise {<state>: "pending"} - SloGS
  • @SloGS, updated the answer - UserName 5:46 pm