There is a code corrected but for some reason the data is not transmitted via AJAX, but it should be recorded 1 and after pressing again write 0 . Displays error.
HERE IS A FILE WITH AJAX:
<?php header('Access-Control-Allow-Origin: *'); if(isset($_GET['result1'])) { if($_GET['result1']=="0") echo '{"ID":"1", "result1":"0"}'; else if($_GET['result1']=="1") echo '{"ID":"1", "result1":"1"}'; } ?> var result1 = 0; var button = document.getElementById('my-button'); var color = true; button.addEventListener( "click" , function(ev){ if (color) { this.style.backgroundColor = '#4cff00' result1 = 1; } else { this.style.backgroundColor = '#6b7077' result1 = 0; } var userId = ev.target.dataset.userId; var userName = ev.target.dataset.userName; var json = JSON.stringify({ ID: userId, surname: userName }); var xhr = new XMLHttpRequest(); xhr.open('GET','http://localhost/tech_user/con_data_ajax/work-ajax-data.php?result1='+result1, true); xhr.send(); document.getElementById("result1").innerHTML = result1; color = !color; }); .cp-pen-styles >input { width:200px; height:50px; } .group-result { position: relative; font-size: 30px; display:inline-block; vertical-align: middle; z-index: 0; padding-left: 10px; padding-right:10px; } <!DOCTYPE html><html lang='en' class=''> <head> <meta charset='UTF-8'> <meta name="robots" content="noindex"> </head> <body> <input id="my-button" type="button" value="WORK" data-user-id ="1" data-user-name= "Копыча"> <div id="result1"class="group-result">0</div> </body> </html>