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> 

  • Please write the error text - goodmice
  • GET con_data_ajax / work-ajax-data.php? Result1 = 1,404 (Not Found) - Karter
  • Rules answer, try it - goodmice am

1 answer 1

Errors that are visible at the moment:
1. The extra closing brace at the end of php
2. A GET request has no body, therefore send should not have any arguments.
3. open has 3 or 5 parameters

xhr.open (method, URL, async [, user, password])

GET request is transmitted in the URL string, for example:

 xhr.open('GET', 'con_data_ajax/work-ajax-data.php?result1='+result1, true); 

Once the request displays 404, then the URL is incorrectly entered or a part is considered for it.

['result1']

Try to specify it completely and / or delete this argument.

Approximate view of the php file:

 <?php //Проверка на существование записи в массиве _GET //В этот массив передаются переменные из GET запроса if(isset($_GET['result1']){ //Проверка значения переменной //PS Желательно обработать переменную прежде чем использовать //Как пример: $result1 = striptags($_GET['result1']); if($_GET['result1']=="0") //Вывод при соблюдении условия echo '{"ID\":"1", "result1":"0"}'; else if($_GET['result1']=="1") //Вывод при не соблюдении 1го, но соблюдении 2го условия echo '{"ID\":"1", "result1":"1"}'; //В ином случае ничего не будет выведено } ?> 

And the type of request:

 xhr.open('GET', 'con_data_ajax/work-ajax-data.php?result1='+result1, true); 

Error 404 means that the page by the specified URL was not found.

  • xhr.open ('GET', 'con_data_ajax / work-ajax-data.php? [' result '], true); So ? - Karter
  • can in pkhp a file something not that? (which I threw off) - Karter
  • @technoimpextechnoimpex Thought it was intended. Firstly, the php file does not give any answer. I will now write in response - goodmice am
  • It's just that I just started to understand this dense forest so to speak, and here such lazhes went (( - Karter
  • Can you fix it by my example? - Karter