I'm currently working on a little PHP-based webapplication and I wish to spice it up a little bit by having the user fill out forms without reloading the entire page. Enter AJAX, the now-infamous Web2.0-demon. I do not want to use prototype and/or Dojo and/or scriptaculous but rather build my own little framework step by step.
Now, I encountered a little problem:
I have a form with 3 little input type="text"-boxes, each one has its unique id in order to compare the user-input to the correct answers in the PHP-script. Whenever I hit the button (no, it's not a normal "submit"-button, but - rather - a "button"-button), the XMLHttpRequest-Object is instantiated, the PHP-script is called, but ... the array is not compared to the user-input.
Here my question:
Do I have to instantiate an XMLHttpRequest-Object for every input-type="text"-box or can I simply loop through all of them?
This is the template:
Code: Select all
<form action="includes/lueckentext1.php" method="get" id="luecke1">
"Ein Projekt ist eine <input type="text" id="aufgabe0" size="10" value="" /> Aufgabe
und ist auf ein zu <input type="text" id="aufgabe1" size="10" />
Ergebnis gerichtet. Es benötigt vielfältige <input type="text" id="aufgabe2" size="10" />
und ist zeitlich begrenzt."<br /><br />
<input type="button" name="send" value="Prüfe!" onClick="baueAnfrage()" />
Code: Select all
function baueAnfrage()
{
var text0, text1, text2;
erzeugeAnfrage();
text0 = document.getElementById("aufgabe0").value;
var url0 = "includes/lueckentext1.php?aufgabe0=" + text0;
anfrage.open("GET", url0, true);
anfrage.onreadystatechange = updateSeite; //KEINE KLAMMERN!
anfrage.send(null);
erzeugeAnfrage();
text1 = document.getElementById("aufgabe1").value;
var url1 = "includes/lueckentext1.php?aufgabe1=" + text1;
anfrage.open("GET", url1, true);
anfrage.onreadystatechange = updateSeite; //KEINE KLAMMERN!
anfrage.send(null);
erzeugeAnfrage();
text2 = document.getElementById("aufgabe2").value;
var url2 = "includes/lueckentext1.php?aufgabe2=" + text2;
anfrage.open("GET", url2, true);
anfrage.onreadystatechange = updateSeite; //KEINE KLAMMERN!
anfrage.send(null);
}
function updateSeite()
{
if (anfrage.readyState == 4 && anfrage.status == 200)
{
var korrigierterText = anfrage.responseText; //was steht im array?
var myH3 = document.createElement("h3"); //element erzeugen
var myText = document.createTextNode(korrigierterText); //textnode erzeugen
myH3.appendChild(myText); //text ans element kleben
var Ausgabebereich = document.getElementById("neuerText"); //und in den entsprechenden div-container...
Ausgabebereich.appendChild(myH3); //reinschreiben...
}
}
Code: Select all
<?php
//uebergabe der get-vars
$eintrag0 = htmlspecialchars($_GET['aufgabe0']);
$eintrag1 = htmlspecialchars($_GET['aufgabe1']);
$eintrag2 = htmlspecialchars($_GET['aufgabe2']);
$korrekt0 = "einmalige";
$korrekt1 = "erzielendes";
$korrekt2 = "Ressourcen";
if (isset ($eintrag0) && $eintrag0 != '')
{
if ($eintrag0 == $korrekt0)
{
echo "Prima! \"$eintrag0\" ist korrekt! ";
}
else
{
echo "Falsch geraten... ";
}
}
else
{
echo "Bitte fuellen Sie alle Felder aus! ";
}
[...]
Thanks for any nerdy help!
