Guest User

Untitled

a guest
Jul 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. var result = 0;
  2. var myEvent = function() {
  3. if (result == 0) {
  4. document.getElementById('my-button').style.backgroundColor = '#4cff00';
  5. result = 1;
  6. } else {
  7. document.getElementById('my-button').style.backgroundColor = '#6b7077';
  8. result = 0;
  9. }
  10. {
  11. var xmlhttp;
  12. if (window.XMLHttpRequest)
  13. {
  14. xmlhttp = new XMLHttpRequest();
  15. var n_JSON = JSON.parse(xmlhttp.responseText);
  16. var ID = n_JSON.ID;
  17. var result = n_JSON.result1;
  18. }
  19. if (ID == 1 && result == 0)
  20. {
  21. var xhr = new XMLHttpRequest();
  22. xhr.open('POST', "con_data_ajax/work-ajax-data.php?user-work="+user+,true);
  23. let body = 'result=' + result;
  24. xhr.send(body);
  25. if (xhr.status == 200) {
  26. document.getElementById('result').innerHTML = result;
  27. }
  28. }
  29. #my-button {
  30. background-color: #6b7077;
  31. }
  32. <button id="my-button" onclick="myEvent()">в работе</button>
  33. <div id="result">0</div>
  34.  
  35. $answer = '{"ID":"0", "Name":"xxx"}';
  36.  
  37. if(($_POST['login']=="tech_user") AND ($_POST['pass']=="111")){
  38.  
  39. $answer = '{"ID":"1", "Name":"Копыча"}';
  40.  
  41. }
  42. if (($_POST['ID']=="1")) {
  43. $answer = '{"result":"1"}' ;
  44. OR
  45. $answer = '{"result":"0"}' ;
  46. }
  47.  
  48. var button = document.getElementById('my-button');
  49.  
  50. button.addEventListener( "click" , function(ev){
  51. if (ev.target.dataset.userId === '1'){
  52. this.style.backgroundColor = '#4cff00'
  53. //Получаем данные по юзеру
  54. var userId = ev.target.dataset.userId;
  55. var userName = ev.target.dataset.userName;
  56. // формируем тело на отправку
  57. var json = JSON.stringify({
  58. id: userId,
  59. surname: userName
  60. });
  61. //Формируете запрос согласно того как у вас настроен сервер.
  62. // var xhr = new XMLHttpRequest();
  63. // xhr.open(.....)
  64. // xhr.send(json)
  65. }else {
  66. this.style.backgroundColor = '#6b7077'
  67. }
  68. ev.target.dataset.userId = 0;
  69. });
Add Comment
Please, Sign In to add comment