Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> AJAX - Zmiana na stronie po zmianie w pliku txt
topcio
post 14.02.2018, 22:44:38
Post #1





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

Ostrzeżenie: (0%)
-----


Hej.

Jak zrobić, by na stornie zmieniał się content po zmianie zawartości pliku txt bez odświeżania strony

[JAVASCRIPT] pobierz, plaintext
  1. window.onload = function () {
  2. if ($('#JavaScript').hasClass('JavaScript_off')) {
  3. $('#JavaScript').removeClass("JavaScript_off");
  4. $('#JavaScript').addClass("JavaScript");
  5. };
  6. $.post('/php_function/set_session_variable.php', { ajax_test: 'javascript' });
  7.  
  8. console.log('OK');
  9. $.ajax({
  10. url: "/test.txt",
  11. dataType: 'json',
  12. success: function(data) {
  13. $( '#testowanie' ).html(data[1]);
  14. },
  15. error: function() {
  16. alert("error");
  17. }
  18. });
  19.  
  20.  
  21.  
  22. };
  23.  
[JAVASCRIPT] pobierz, plaintext


Napisałem coś takiego i wiem że problem jest w tym, że odświeża to po załadowaniu dokumentu.
Ale jak to zrobić by samo się przeładowywało?






Ok już chyba znalazłem rozwiązanie. gotowiec z neta, ale działa

[JAVASCRIPT] pobierz, plaintext
  1. var previous = "";
  2.  
  3. setInterval(function() {
  4. var ajax = new XMLHttpRequest();
  5. ajax.onreadystatechange = function() {
  6. if (ajax.readyState == 4) {
  7. if (ajax.responseText != previous) {
  8. alert("file changed!");
  9. previous = ajax.responseText;
  10. }
  11. }
  12. };
  13. ajax.open("POST", "/test.txt", true);
  14. ajax.send();
  15. }, 1000);
  16.  
[JAVASCRIPT] pobierz, plaintext


Zrobiłem, działa dobrze

[JAVASCRIPT] pobierz, plaintext
  1. setInterval(function() {
  2. var previous = "";
  3. var ajax = new XMLHttpRequest();
  4. ajax.onreadystatechange = function() {
  5. if (ajax.readyState == 4) {
  6. if (ajax.responseText != previous) {
  7. var data = JSON.parse(ajax.responseText);
  8. var id = $(this);
  9. $( '#testowanie' ).html(data[1]);
  10. previous = ajax.responseText;
  11. }
  12. }
  13. };
  14. ajax.open("post", "/test.txt", true);
  15. ajax.send();
  16. }, 1000);
[JAVASCRIPT] pobierz, plaintext


Ale teraz mam inny problem.

Już opisuję:
Na stronie pobierane są dane z sqla i dla każdego wyniku tworzony jest div o ID odpowiadającym ID z bazy plus suma kontrolna, np ID sesji, albo konkretna tekst (jeszcze nie zdecydowałem).
Jak zrobić, by powyższa funkcja działała dla każdego diva.
Dla każdego z nich jest oddzielny plik zamiast test.txt jest to indywidualna nazwa (również pobierana z bazy).


Go to the top of the page
+Quote Post
trzczy
post 14.02.2018, 23:31:52
Post #2





Grupa: Zarejestrowani
Postów: 460
Pomógł: 49
Dołączył: 5.06.2011

Ostrzeżenie: (0%)
-----


https://stackoverflow.com/a/44036573/5673289

Musisz skompletować tablicę tych identyfikatorów i wykorzystać parametr value tej funkcji anonimowej

Ten post edytował trzczy 14.02.2018, 23:33:38
Go to the top of the page
+Quote Post
topcio
post 15.02.2018, 01:31:33
Post #3





Grupa: Zarejestrowani
Postów: 140
Pomógł: 0
Dołączył: 14.01.2017

Ostrzeżenie: (0%)
-----


Zrobiłem tak
[JAVASCRIPT] pobierz, plaintext
  1. setInterval(function() {
  2. var data = mysql_query("test");
  3. for (var i = 0; i < data.length; i++ ) {
  4. console.log(data[i]['dvr_mac_address']);
  5. var previous = "";
  6. var ajax = new XMLHttpRequest();
  7. ajax.onreadystatechange = function() {
  8. if (ajax.readyState == 4) {
  9. if (ajax.responseText != previous) {
  10. var data_new = JSON.parse(ajax.responseText);
  11. $( '#' + data[i]['dvr_mac_address'] ).html(data_new);
  12. previous = ajax.responseText;
  13. }
  14. }
  15. };
  16. ajax.open("post", "/" + data[i]['dvr_mac_address'], true);
  17. ajax.send();
  18. }
  19. }, 1000);
  20.  
  21.  
  22. function mysql_query(query, args = null) {
  23. var json = null;
  24. $.ajax({
  25. type: "POST",
  26. async: false,
  27. url: "/php_function/set_session_variable.php",
  28. data: {ajax_test: 'dvr_status', query: query, args: args},
  29. success: function(response){
  30. json = JSON.parse(response);
  31. }
  32. });
  33. return json;
  34. }
[JAVASCRIPT] pobierz, plaintext

  1. if ($_POST['ajax_test'] == 'dvr_status') {
  2. $query = $_POST['query'];
  3. $args = $_POST['args'];
  4. $real_query = null;
  5. if( $query == "test" ) {
  6. $real_query = $dvr->get_active_dvr();
  7. }
  8. if ($real_query != null ) {
  9. echo json_encode($real_query);
  10. }
  11. }


i funkcja w klasie dvr

  1. public function get_active_dvr() {
  2. $result = $this->sql->query("SELECT dvr_mac_address FROM dvr WHERE dvr_active_status = 1");
  3. $results = [];
  4. while($data = $result->fetch_assoc()) {
  5. array_push($results, $data);
  6. }
  7. return $results;
  8. }


Czy tak może być?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 23:51