Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript][PHP]Kilka odświeżaczy divów
Forum PHP.pl > Forum > Przedszkole
koxu1996
Witam. Mam na stronie diva który się odświeża i pokazuje logi. Chciałem zrobić jeszcze dynamiczny div pokazujący liczbę pozostałych punktów. Jednak po dodaniu drugiego takiego "odświerzacza" na stronie oba przestały działać :/

Jak użyć kilku poniższych skryptów aby działały?

KOD
index.php
  1. <script src="ajax.js"></script>
  2. <strong>Logi</strong>
  3. <script type="text/javascript"><!--
  4. refreshdiv();
  5. // --></script>
  6. <div id="timediv"></div>


ajax.js
  1. // Customise those settings
  2.  
  3. var seconds = 1;
  4. var divid = "timediv";
  5. var url = "boo.php";
  6.  
  7. ////////////////////////////////
  8. //
  9. // Refreshing the DIV
  10. //
  11. ////////////////////////////////
  12.  
  13. function refreshdiv(){
  14.  
  15. // The XMLHttpRequest object
  16.  
  17. var xmlHttp;
  18. try{
  19. xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
  20. }
  21. catch (e){
  22. try{
  23. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
  24. }
  25. catch (e){
  26. try{
  27. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  28. }
  29. catch (e){
  30. alert("Your browser does not support AJAX.");
  31. return false;
  32. }
  33. }
  34. }
  35.  
  36. // Timestamp for preventing IE caching the GET request
  37.  
  38. fetch_unix_timestamp = function()
  39. {
  40. return parseInt(new Date().getTime().toString().substring(0, 10))
  41. }
  42.  
  43. var timestamp = fetch_unix_timestamp();
  44. var nocacheurl = url+"?t="+timestamp;
  45.  
  46. // The code...
  47.  
  48. xmlHttp.onreadystatechange=function(){
  49. if(xmlHttp.readyState==4){
  50. document.getElementById(divid).innerHTML=xmlHttp.responseText;
  51. setTimeout('refreshdiv()',seconds*1000);
  52. }
  53. }
  54. xmlHttp.open("GET",nocacheurl,true);
  55. xmlHttp.send(null);
  56. }
  57.  
  58. // Start the refreshing process
  59.  
  60. var seconds;
  61. window.onload = function startrefresh(){
  62. //setTimeout('refreshdiv()',seconds*1000);
  63. setTimeout('refreshdiv()',seconds*500);
  64. }


boo.php
  1. <?php
  2.  
  3. // Configure connection settings
  4. $host="xxxxxx"; // Nazwa hosta.
  5. $db = 'xxxxxx';
  6. $db_admin = 'xxxxx';
  7. $db_password = 'xxxxxxx';
  8. $tablename = 'test';
  9.  
  10.  
  11.  
  12. // Title
  13.  
  14. //echo "Contents of the table:";
  15.  
  16. // Connect to DB
  17.  
  18. $sql = mysql_connect($host, $db_admin, $db_password)
  19.  
  20. mysql_select_db("$db", $sql);
  21.  
  22. // Fetch the data
  23.  
  24. //$query = "SELECT * FROM $tablename ORDER BY id DESC";
  25. ///$query = "SELECT * FROM $tablename";
  26. //$result = mysql_query($query);
  27.  
  28. // Return the results, loop through them and echo
  29. $result = mysql_query("SELECT * FROM $tablename ORDER BY id DESC LIMIT 8",$sql)
  30. or die ("nie wykonalem");
  31.  
  32. while ($roww = mysql_fetch_row($result)) {
  33. echo ' '.$roww[1].' '.$roww[2].' '.$roww[3].' '.$roww[4] .'<br>' ;
  34. }
  35.  
  36. ?>


Może lepiej to będzie zrobić w jQuery?
erix
Dump z konsoli błędów albo zamykam temat.
koxu1996
Ale błędu nie wyświetla. Po prostu jest pusto i nie pokazuje logów ani punktów.
erix
Wystaw gdzieś to na żywo, bo debugowanie JS-a z listingu, to jak prowadzenie samochodu tylko teoretycznie.
koxu1996
Strona: http://project-welcome.ugu.pl/test/original.php
strona odpowiadajaca za logi: http://project-welcome.ugu.pl/test/boo.php + ajax.js
strona odpowiadajaca za punkty: http://project-welcome.ugu.pl/test/boo2.php + ajax2.js

Na stronie trzeba się zalogować:

Login: testowyuser
Hasło: testtest


Tam gdzie jest rozwalona linia tam jest div z punktam. Na dole jest div z logami i on działa prawdopodobnie dlatego że wczytanie pliku .js jest później.

Może ktoś wie jak połączyć oba pliki.js żeby to funkcjonowało?
erix
A po co duplikujesz ten sam kod w obu plikach...?
koxu1996
Bo mam dwa różne divy, jeden korzysta z boo.php a drugi z boo2.php (te pliki sie różnią). Nie mam pojęcia jak zrobić to w jednym pliku chyba że Ty wiesz.
erix
Masz dwa razy identyczny kod, choćby od inicjalizacji AJAX.
koxu1996
Niestety nie za bardzo rozkminiam o co chodzi. Czy mógłbyś połączyć te kody, bo ja niestety nie potrafię?
erix
To nie jest dział na gotowce.
koxu1996
Dobra, udało mi się zrobić takie coś:

  1. // Customise those settings
  2.  
  3. var seconds = 1;
  4. var divid = "timediv";
  5. var divid2 = "points";
  6. var url = "boo.php";
  7. var url2 = "boo2.php";
  8.  
  9. ////////////////////////////////
  10. //
  11. // Refreshing the DIV
  12. //
  13. ////////////////////////////////
  14.  
  15. function refreshdiv(){
  16.  
  17. // The XMLHttpRequest object
  18.  
  19. var xmlHttp;
  20. try{
  21. xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
  22. }
  23. catch (e){
  24. try{
  25. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
  26. }
  27. catch (e){
  28. try{
  29. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  30. }
  31. catch (e){
  32. alert("Your browser does not support AJAX.");
  33. return false;
  34. }
  35. }
  36. }
  37.  
  38. // Timestamp for preventing IE caching the GET request
  39.  
  40. fetch_unix_timestamp = function()
  41. {
  42. return parseInt(new Date().getTime().toString().substring(0, 10))
  43. }
  44.  
  45. var timestamp = fetch_unix_timestamp();
  46. var nocacheurl = url+"?t="+timestamp;
  47. var nocacheurl2 = url2+"?t="+timestamp;
  48. // The code...
  49.  
  50. xmlHttp.onreadystatechange=function(){
  51. if(xmlHttp.readyState==4){
  52. document.getElementById(divid).innerHTML=xmlHttp.responseText;
  53. document.getElementById(divid2).innerHTML=xmlHttp.responseText;
  54. setTimeout('refreshdiv()',seconds*1000);
  55. }
  56. }
  57. xmlHttp.open("GET",nocacheurl,true);
  58. xmlHttp.send(null);
  59. xmlHttp.open("GET",nocacheurl2,true);
  60. xmlHttp.send(null);
  61. }
  62.  
  63. // Start the refreshing process
  64.  
  65. var seconds;
  66. window.onload = function startrefresh(){
  67. setTimeout('refreshdiv()',seconds*1000);
  68. }


Jednak coś jeszcze nie gra bo na stronie w obu divach wyświetlają się punkty. Gdzie mam jeszcze poprawić kod?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.