Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Problem z czatem, jw
miras
post 14.01.2015, 17:26:56
Post #1





Grupa: Zarejestrowani
Postów: 1 004
Pomógł: 9
Dołączył: 18.01.2011
Skąd: Siedlce

Ostrzeżenie: (30%)
XX---


Witam, problem jest następujący - mam pewien czat, na którym wiadomości wyświetlają się dopiero po podaniu nazwy użytkownika, a ja chciałbym, żeby wiadomości czatu wyświetlały się od razu, tylko jak ktoś będzie chciał napisać wiadomość to będzie musiał podać ten nick.

tutaj jest główny kod js:

http://wklej.org/hash/15b83e7bf12/

wiadomości wyświetlane są w tym divie:

  1. <div id="messages"></div>


według mnie trzeba edytować tą funkcję (próbowałem na wszystkie sposoby, które przyszły mi do głowy)

  1. function chat_msgs_get()
  2. {
  3. chat_tout = setTimeout("chat_msgs_get();", Math.round(1000*chat_timeout));
  4. if (chat_XMLHttp_get.readyState % 4) return;
  5. chat_rand += 1;
  6. chat_XMLHttp_get.open("get", chat_path+"php/msg_get.php?rand="+chat_rand+
  7. "&room="+encodeURIComponent(chat_room)+
  8. "&user="+encodeURIComponent(chat_user)+
  9. "&pass="+encodeURIComponent(chat_pass)+
  10. "&mptr="+chat_mptr);
  11. chat_XMLHttp_get.send(null);
  12. chat_XMLHttp_get.onreadystatechange = function()
  13. {
  14. if(chat_XMLHttp_get.readyState == 4 && chat_XMLHttp_get.status == 200)
  15. {
  16. document.getElementById('log_get').innerHTML = chat_XMLHttp_get.responseText;
  17.  
  18. var data = chat_parse(chat_XMLHttp_get.responseText);
  19. if (data[0] == '-' && chat_user && chat_pass) { chat_api_onload(chat_room, true); return; }
  20. for (var i = 1; i < data.length-1; i++)
  21. {
  22. chat_mptr = Math.max(chat_mptr, data[i]);
  23.  
  24. if (data[i+1] == '+')
  25. {
  26. if (chat_smsg) if (data[i+2] == chat_room) chat_msgs['.'] += '<b>System:</b> user <b>'+chat_msgs_usr(data[i+3], 'black', false)+'</b> enters the room<br />';
  27. chat_usrs[data[i+3]] = new Array(data[i+2], data[i+4], data[i+5], true);
  28. i += 5;
  29. }
  30.  
  31. if (data[i+1] == '-')
  32. {
  33. if (chat_smsg) if (data[i+2] == chat_room) chat_msgs['.'] += '<b>System:</b> user <b>'+chat_msgs_usr(data[i+3], 'black', false)+'</b> leaves the room<br />';
  34. chat_usrs[data[i+3]] = false;
  35. i += 3;
  36. }
  37.  
  38. if (data[i+1] == 's')
  39. {
  40. if (chat_usrs[data[i+2]]) chat_usrs[data[i+2]][3] = data[i+3] == '+';
  41. i += 3;
  42. }
  43.  
  44. if (data[i+1] == 'm')
  45. {
  46. chat_usrs[data[i+5]] = new Array(chat_room, data[i+3], data[i+4], true);
  47. data[i+7] = data[i+7].replace(/%%(\w+)%%/g, '<img src="'+chat_path+'smileys/$1.gif" alt="" />');
  48. if (data[i+6] == '.')
  49. chat_msgs['.'] += '<span style="color: '+data[i+2]+'"><b>['+chat_date(-data[i+8])+'] '+chat_msgs_usr(data[i+5], data[i+2])+'</b>: '+data[i+7]+'</span><br />';
  50. else
  51. {
  52. chat_priv_prepair(data[i+5], data[i+6]);
  53. chat_msgs[data[i+5]][data[i+6]] += '<span style="color: '+data[i+2]+'"><b>['+chat_date(-data[i+8])+'] '+chat_msgs_usr(data[i+5], data[i+2])+'</b>: '+data[i+7]+'</span><br />';
  54. chat_msgs[data[i+6]][data[i+5]] += '<span style="color: '+data[i+2]+'"><b>['+chat_date(-data[i+8])+'] '+chat_msgs_usr(data[i+5], data[i+2])+'</b>: '+data[i+7]+'</span><br />';
  55. chat_wait[data[i+5]][data[i+6]] = false;
  56. chat_wait[data[i+6]][data[i+5]] = true;
  57. }
  58. i += 8;
  59. }
  60. }
  61.  
  62. if (data.length > 1)
  63. {
  64. chat_out_msgs();
  65. chat_out_usrs();
  66. }
  67. }
  68. }
  69. }
  70.  




Proszę o jakieś wskazówki, bo męczę się z tym już kilka godzin i pewnie jeszcze kilka się pomęcze smile.gif

Dzięki z góry!


--------------------
Go to the top of the page
+Quote Post
trueblue
post 14.01.2015, 19:06:50
Post #2





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


1. W linii 6 przesyłasz do skryptu php nazwę i hasło użytkownika, być może następuje jakaś weryfikacja tych zmiennych w skrypcie.
2. W linii sprawdzasz czy nazwa i hasło użytkownika są zdefiniowane i niepuste.
3. Potem wewnątrz warunków używasz zmiennej data[i+3], która wygląda na to, że przechowuje nazwę użytkownika zwróconą przez skrypt php.


--------------------
Go to the top of the page
+Quote Post
miras
post 14.01.2015, 22:39:09
Post #3





Grupa: Zarejestrowani
Postów: 1 004
Pomógł: 9
Dołączył: 18.01.2011
Skąd: Siedlce

Ostrzeżenie: (30%)
XX---


Problem jednak delikatnie uległ zmianie, może mi ktoś powiedzieć dlaczego to nie działa:

  1. <script type="text/javascript">
  2. window.onload = function(){
  3. chat_msgs_add();
  4. return false;}
  5. }


a to działa..

  1. <form class="send" action="" onsubmit="chat_msgs_add(); return false;">


?


--------------------
Go to the top of the page
+Quote Post
trueblue
post 14.01.2015, 22:48:46
Post #4





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Wrzuć alert(1) przed return false; w pierwszym skrypcie. Jeśli nie działa, to znaczy że inny skrypt nadpisuje zdarzenie onload.


--------------------
Go to the top of the page
+Quote Post
miras
post 14.01.2015, 22:49:58
Post #5





Grupa: Zarejestrowani
Postów: 1 004
Pomógł: 9
Dołączył: 18.01.2011
Skąd: Siedlce

Ostrzeżenie: (30%)
XX---


alert normalnie wyskakuje...


--------------------
Go to the top of the page
+Quote Post
trueblue
post 14.01.2015, 22:55:13
Post #6





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


To sprawdź czy funkcja chat_msgs_add, wymaga jakichś zmiennych, których nie masz zdefiniowanych podczas onload, ale masz je w onsubmit.


--------------------
Go to the top of the page
+Quote Post
miras
post 14.01.2015, 22:59:38
Post #7





Grupa: Zarejestrowani
Postów: 1 004
Pomógł: 9
Dołączył: 18.01.2011
Skąd: Siedlce

Ostrzeżenie: (30%)
XX---


Właśnie nie.. załączam kod funkcji:

  1. function chat_msgs_add()
  2. {
  3. if (!chat_user || !chat_pass) { chat_login(false); return; }
  4. if (!document.getElementById('send').value || chat_XMLHttp_add.readyState % 4) return;
  5.  
  6. chat_rand += 1;
  7. chat_XMLHttp_add.open("get", chat_path+"php/msg_add.php?rand="+chat_rand+
  8. "&user="+encodeURIComponent(chat_user)+
  9. "&pass="+encodeURIComponent(chat_pass)+
  10. "&priv="+encodeURIComponent(chat_priv)+
  11. "&colr="+encodeURIComponent(chat_colr)+
  12. "&data="+encodeURIComponent(document.getElementById('send').value));
  13. chat_XMLHttp_add.send(null);
  14. chat_XMLHttp_add.onreadystatechange = function()
  15. {
  16. if(chat_XMLHttp_add.readyState == 4 && chat_XMLHttp_add.status == 200)
  17. {
  18. document.getElementById('log_add').innerHTML = chat_XMLHttp_add.responseText;
  19.  
  20. if (chat_XMLHttp_add.responseText)
  21. {
  22. chat_msgs['.'] += '<b>System:</b> '+chat_XMLHttp_add.responseText+'<br />';
  23. chat_out_msgs();
  24. }
  25. }
  26. }
  27.  
  28. document.getElementById('send').value = '';
  29. if (chat_focu) document.getElementById('send').focus();
  30. }
  31.  


--------------------
Go to the top of the page
+Quote Post
trueblue
post 15.01.2015, 09:17:53
Post #8





Grupa: Zarejestrowani
Postów: 6 806
Pomógł: 1828
Dołączył: 11.03.2014

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


Jako argumentów nie wymaga, to widać przy jej wywołaniu, ale w linii 3 i 4 są warunki na zmienne.


--------------------
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: 19.07.2025 - 06:46