Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [AJAX] format odpowiedzi, string -> html
Francislaw
post 5.12.2014, 16:14:55
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 1.09.2014

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


Hejka,

Skleciłem jakoś kod który pobiera z php output z echo. Wygląda on tak:

  1. <!DOCTYPE html
  2. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl">
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
  6. <title>Ajax script</title>
  7. <script type="text/javascript">
  8. function getXMLHttpRequest()
  9. {
  10. var request = false;
  11.  
  12. try {
  13. request = new XMLHttpRequest();
  14. } catch(err1) {
  15. try {
  16. request = new ActiveXObject('Msxml2.XMLHTTP');
  17. } catch(err2) {
  18. try {
  19. request = new ActiveXObject('Microsoft.XMLHTTP');
  20. } catch(err3) {
  21. request = false;
  22. }
  23. }
  24. }
  25. return request;
  26. }
  27. </script>
  28. </head>
  29. <script type="text/javascript">
  30. function wyslij()
  31. {
  32. var e;
  33. var div = document.getElementById("output");
  34. var p = document.getElementById('sz').value;
  35. var r;
  36. r = getXMLHttpRequest();
  37. r.open('POST', 'test.php', true);
  38. r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  39. r.send("login="+p);
  40. r.onreadystatechange = processResponse;
  41. function processResponse()
  42. {
  43. if (r.readyState == 4) {
  44. if (r.status == 200) {
  45. div.textContent = r.responseText;
  46. };
  47. };
  48. }
  49. }
  50. </script>
  51. <a id="guzilk" href="#" onclick="wyslij();">Pobierz dane</a>
  52. <input type="text" id="sz" value="login"/>
  53. <div id="output">Pusto</div>
  54.  
  55. </body>
  56. </html>


Wysyła wpisany login do php, a php wyszukuje info o nim w bazie danych i zwraca rezultaty w formie echo.

No i działa, problem polega na tym, że w JS w odpowiedzi dostaję kod jako string. Czyli dla tekstu przykład dostaję <b>przykład</b>, a chciałbym, żeby po prostu html został "zaaplikowany" i sformatował tekst.

Szukałem jakichś funkcji na to ale nic nie działa. Wie może ktoś jak to zrobić ?
Go to the top of the page
+Quote Post
Turson
post 5.12.2014, 16:18:40
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Kod
div.innerHTML = r.responseText;
Go to the top of the page
+Quote Post
Francislaw
post 5.12.2014, 16:20:54
Post #3





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 1.09.2014

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


Działa ! Dzięki smile.gif

Ten post edytował Francislaw 5.12.2014, 16:22:44
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: 10.06.2024 - 06:26