Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [ajax] Skrypt nie działa (?)
--void--
post
Post #1





Goście







Kod
var pt=0;
function getQuestions() {
   obj=document.getElementById("question");
   obj.firstChild.nodeValue="(please wait)";
   ajaxCallback = nextQuestion;
   ajaxRequest("questions.xml");
}

function nextQuestion() {
   questions = ajaxreq.responseXML.getElementsByTagName("q");
   obj=document.getElementById("question");
   if (pt < questions.length) {
      q = questions[pt].firstChild.nodeValue;
      obj.firstChild.nodeValue=q;
   } else {
      obj.firstChild.nodeValue="(no more questions)";
   }
}


Bardzo proszę o pomoc. Ten fragment skryptu nie działa, konkretnie nie pobiera danych z pliku questions.xml, który wygląda tak:
Kod
<?xml version="1.0" ?>
<questions>
<q>Pytanie</q>
</a>Odpowiedź</a>
</questions>


(IMG:http://forum.php.pl/style_emoticons/default/blinksmiley.gif)

Pozdrawiam.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
drPayton
post
Post #2





Grupa: Zarejestrowani
Postów: 890
Pomógł: 65
Dołączył: 13.11.2005
Skąd: Olsztyn

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


To co się natychmiast rzuca w oczy, to:
  1. </a>Odpowiedź</a>

  1. <a>Odpowiedź</a>

chyba raczej...

Ten post edytował drPayton 12.10.2007, 22:51:36
Go to the top of the page
+Quote Post
--void--
post
Post #3





Goście







Po prostu pomyliłem się przy przepisywaniu.
Jeśli nic innego się nie rzuca w oczy, to napiszę całą aplikację:

Kod ajaxquiz.html
  1. <head><title>Test AJAX</title>
  2. <script language="JavaScript" type="text/javascript"
  3. src="ajax.js">
  4. </head>
  5. <h1>Przykład quizu w technice AJAX</h1>
  6. <p><b>Pytanie:</b>
  7. <span id="question">...
  8. </span>
  9. </p>
  10. <p><b>Odpowiedź:</b>
  11. <input type="text" name="answer" id="answer">
  12. <input type="button" value="Wyślij" id="submit">
  13. </p>
  14. <input type="button" value="Zacznij test" id="startq">
  15. </form>
  16. <script language="JavaScript" type="text/javascript"
  17. src="quiz.js">
  18. </body>
  19. </html>


Kod ajax.js:
Kod
// zmienne globalne śledzące żądanie
// i funkcję do wywołania na koniec
var ajaxreq=false, ajaxCallback;
// ajaxRequest: tworzy żądanie
function ajaxRequest(filename) {
   try {
    // Firefox / IE7 / inne
    ajaxreq= new XMLHttpRequest();
   } catch (error) {
    try {
      // IE 5 / IE 6
      ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (error) {
      return false;
    }
   }
   ajaxreq.open("GET",filename);
   ajaxreq.onreadystatechange = ajaxResponse;
   ajaxreq.send(null);
}
// ajaxResponse: czeka na odpowiedź i wywołuje funkcję
function ajaxResponse() {
   if (ajaxreq.readyState !=4) return;
   if (ajaxreq.status==200) {
      // jeśli żądanie się powiodło...
      if (ajaxCallback) ajaxCallback();
   } else alert("Błąd żądania: " + ajaxreq.statusText);
   return true;
}


Kod questions.xml:
Kod
<?xml version="1.0" ?>
<questions>
    <q>Pytanie</q>
    <a>Odpowiedź</a>
</questions>



Proszę o pomoc, bo zależy mi na tym skrypcie...
Go to the top of the page
+Quote Post
--void--
post
Post #4





Goście







ehh, jeszcze quiz.js

Kod
// zmienna globalna qn jest numerem bieżącego pytania
var qn=0;
// wczytaj pytania z pliku XML
function getQuestions() {
   obj=document.getElementById("question");
   obj.firstChild.nodeValue="(please wait)";
   ajaxCallback = nextQuestion;
   ajaxRequest("questions.xml");
}
// wyświetl następne pytanie
function nextQuestion() {
   questions = ajaxreq.responseXML.getElementsByTagName("q");
   obj=document.getElementById("question");
   if (qn < questions.length) {
      q = questions[qn].firstChild.nodeValue;
      obj.firstChild.nodeValue=q;
   } else {
      obj.firstChild.nodeValue="(no more questions)";
   }
}
// sprawdź odpowiedź użytkownika
function checkAnswer() {
   answers = ajaxreq.responseXML.getElementsByTagName("a");
   a = answers[qn].firstChild.nodeValue;
   answerfield = document.getElementById("answer");
   if (a == answerfield.value) {
      alert("Dobrze!");
   }
   else {
      alert("Źle. Poprawna odpowiedź brzmi: " + a);
   }
   qn = qn + 1;
   answerfield.value="";
   nextQuestion();
}
// Ustaw funkcje obsługi zdarzeń dla przycisków
obj=document.getElementById("startq");
obj.onclick=getQuestions;
ans=document.getElementById("submit");
ans.onclick=checkAnswer;
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 15.09.2025 - 15:22