2016-10-17 6 views
1

私は、XMLドキュメントへの投稿、フォームを生成しようとしているが、コードが他のオプションを服用し続けるで声明「あなたは正しくフィールドに入力を確認してください。」私が変数を適切に提供したときに、registration.phpファイルに書き込んでください。 html、javascript、phpの3つのセクションがあります。私は取得しています出力は、私はあなたが鍵を欠けていると思う変数が読み込まれていない場合は、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は、彼らがすべてある場合は、1 'isset'の内側にテストするために複数のフィールドを配置することができます'タグ... '

関連する問題