What should the php page look like that will answer such a question of the type true or false (it can be something else), checking whether the given spisok.txt has this id or not.

In the file id written in one line separated by commas!

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 } })(); xmlHttpRequest.sendRequest("http://site.ru/test.php?id=21312").then(function (result) { alert(result); }); 

    1 answer 1

    a.php

     if(isset($_POST['id'])) { $id = $_POST['id']; $found = false; $file = file_get_contents('spisok.txt'); $id_list = explode(",", $file); foreach($id_list as $i => $value) { if($value == $id) { $found = true; } } echo $found ? 'true' : 'false'; } 

    script.js

     xhr = new XMLHttpRequest(); xhr.open('POST', 'a.php', true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send('id=' + 21312); xhr.onreadystatechange = function() { if(xhr.readyState == 4) { if(xhr.status == 200) { console.log(xhr.responseText); // false or true } } } 

    spisok.txt

     1,2,3,4,5,21312,56 
    • For the GET request xhr.open('GET', 'a.php?id=21312', true); xhr.send(); - Mr. Black