2016-10-17 1 views
1

Ich versuche, ein Formular zu generieren, in einem XML-Dokument, aber der Code nimmt weiterhin die sonst Option "Bitte stellen Sie sicher, dass Sie die Felder korrekt ausgefüllt." in der Datei registration.php, wenn ich die Variablen richtig geliefert habe. Es gibt 3 Abschnitte: HTML, Javascript und PHP. Der Ausgang Ich erhalte istVariablen werden nicht eingelesen, wenn Anweisung in PHP

<html> 
    <head> 
    <title>A Simple Example</title> 
    </head> 
    <body> 
    <script src="clientSideScripts.js"></script> 
    <h1>Registration Page </h1> 
    <table border="0"> 
    <form> 
    <tr> 
     <td><strong>Firstname:</strong></td> 
     <td> 
      <input type="text" name="firstname" id="firstname" required /> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Lastname:</strong></td> 
     <td> 
      <input type="text" name="lastname" id="lastname" required /> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Password:</strong></td> 
     <td> 
     <input type="password" id="userPassword" name="password" required/> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Confirm Password:</strong></td> 
     <td> 
     <input type="password" id="confirmPassword" name="pwdfield" required/> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Email:</strong></td> 
     <td> 
     <input type="email" name="email" id="email" value="" required pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,4}$" /> 
     </td> 
    </tr> 
    <tr> 
     <td><strong>Contact Phone:</strong></td> 
     <td> 
     <input type="text" name="phone" id="phone" value=""/> 
     </td> 
    </tr> 
    <td> 
     <input type="button" value="Register" onclick="registerCustomer();"/> 
    </td> 
    </form> 
    <div id="information"/> 
</body> 
</html> 


var xHRObject = false; 

if (window.XMLHttpRequest) 
    xHRObject = new XMLHttpRequest(); 
else if (window.ActiveXObject) 
    xHRObject = new ActiveXObject("Microsoft.XMLHTTP"); 

function validatePassword() 
{ 
     var a = document.getElementById("userPassword"); 
     var b=document.getElementById("confirmPassword"); 
     return a.value == b.value; 
} 

function registerCustomer() 
{ 
    if(!validatePassword()) 
     alert("The passwords don't match."); 
    else 
    { 
     var firstname = document.getElementById("firstname").value; 
     var lastname = document.getElementById("lastname").value; 
     var password = document.getElementById("userPassword").value; 
     var email = document.getElementById("email").value; 
     var phone = document.getElementById("phone").value; 
     var url = "registration.php?firstname=" + firstname + "&lastname" + lastname + "&password=" + password + "&email=" + email + "&phone=" + phone; 
     xHRObject.open("GET", url , true); 
     xHRObject.onreadystatechange = function() 
     { 
      if (xHRObject.readyState == 4 && xHRObject.status == 200) 
       document.getElementById('information').innerHTML = xHRObject.responseText + "<br><a href='buyonline.htm'>back</a>"; 
     } 
     xHRObject.send(null); 
    } 
} 


function customerLogin() 
{ 
     var email = document.getElementById("email").value; 
     var password = document.getElementById("password").value; 
     var url = "login.php?email=" + email + "&password=" + password; 
     xHRObject.open("GET", url , true); 
     xHRObject.onreadystatechange = function() 
     { 
      if (xHRObject.readyState == 4 && xHRObject.status == 200) 
       document.getElementById('information').innerHTML = xHRObject.responseText + "<br><a href='buyonline.htm'>back</a>"; 
     } 
     xHRObject.send(null); 
} 

<?php 
if(isset($_GET["firstname"]) && isset($_GET["lastname"]) && isset($_GET["password"]) && isset($_GET["email"])) 
{ 
    if(isEmailUnique()) 
     insertCustomer(); 
    else 
     echo "This email is already taken, please choose another one."; 
} 
else echo "Please make sure you filled in the fields correctly."; 

function getLastId(){ 
    $id = 0; 
    $xmlFile = "../../data/customer.xml"; 
    try 
    { 
     $dom = DOMDocument::load($xmlFile); 
     $customers = $dom->getElementsByTagName("customer"); 

     foreach($customers as $node) 
     { 
      $cID = $node->getElementsByTagName("id"); 
      $cID = $cID->item(0)->nodeValue; 
      if (($id < $cID)) $id = $cID; 
     } 
    } 
    catch(Exception $e) 
    { 
     $doc = new DomDocument('1.0'); 
     $customers = $doc->createElement('customers'); 
     $customers = $doc->appendChild($customers); 

     $doc->saveXML(); 
     return 1; 
    } 
    return $id; 
} 

function insertCustomer() 
{ 
    try { 
     $xmlFile = "../../data/customer.xml"; 
     $doc = DOMDocument::load($xmlFile); 
     $doc->formatOutput = true; 
     $customer = $doc->createElement("customer"); 

     $Customers = $doc->getElementsByTagName("Customers"); 


     $customer = $Customers->item(0)->appendChild($customer); 
     $newID = getLastId() + 1; 
     $id = $doc->createElement('id'); 
     $idValue = $doc->createTextNode($newID); 
     $id->appendChild($idValue); 
     $customer->appendChild($id); 


     $name1 = $doc->createElement('firstname'); 
     $nameValue = $doc->createTextNode($_GET["firstname"]); 
     $value2 = $name1->appendChild($nameValue); 
     $name = $customer->appendChild($name1); 

     $name = $doc->createElement('lastname'); 
     $nameValue = $doc->createTextNode($_GET["lastname"]); 
     $value2 = $name->appendChild($nameValue); 
     $name = $customer->appendChild($name); 

     $name = $doc->createElement('password'); 
     $nameValue = $doc->createTextNode($_GET["password"]); 
     $value2 = $name->appendChild($nameValue); 
     $name = $customer->appendChild($name); 


     $name = $doc->createElement('email'); 
     $nameValue = $doc->createTextNode($_GET["email"]); 
     $value2 = $name->appendChild($nameValue); 
     $name = $customer->appendChild($name); 

     $name = $doc->createElement('phone'); 
     $nameValue = $doc->createTextNode($_GET["phone"]); 
     $value2 = $name->appendChild($nameValue); 
     $name = $customer->appendChild($name); 

     echo "<xmp>".$doc->save($xmlFile)."</xmp>"; 

    } 
    catch(Exception $e) { echo $e;} 

    echo "customer successfully registered and your new Id = ". $newID; 
} 


function isEmailUnique() 
{ 
    $xmlFile = "../../data/customer.xml"; 
    try 
    { 
     $dom = DOMDocument::load($xmlFile); 
     $customers = $dom->getElementsByTagName("customer"); 

     foreach($customers as $node) 
     { 
      $email = $node->getElementsByTagName("email"); 
      $email = $email->item(0)->nodeValue; 

      if (($email == $_GET["email"])) return false; 
     } 
    } 
    catch(Exception $e) 
    { 
     $doc = new DomDocument('1.0'); 
     $customers = $doc->createElement('customers'); 
     $customers = $doc->appendChild($customers); 

     $doc->saveXML(); 
     return true;  
    } 
    return true; 
} 
?> 
+0

JAVASCRIPT müssen innen '' Tags und sollte eigentlich auch in der ' ....' Tags sein – RiggsFolly

+0

Sie können mehrere Felder platzieren innerhalb eines 'isset' zu testen, ob sie alle UND testet 'isset ($ _ GET [" Vorname "], $ _GET [" Nachname "], $ _GET [" Passwort "], $ _GET [" E-Mail "])' – RiggsFolly

Antwort

3

Ich glaube, Sie einen Schlüssel Gleichen sind vermisst hier anmelden (innen registerCustomer()-Funktion):

"...firstname=" + firstname + "&lastname" + lastname + "&password=" 

Kontrast mit:

"...firstname=" + firstname + "&lastname=" + lastname + "&password=" 

Dies bedeutet, Der GET-Index lastname wird niemals festgelegt.

+0

Vielen Dank, es funktioniert :)! –

Verwandte Themen