2013-10-19 88 views
0

天兒真好,附加文本到XML

由於我的任務的一部分,我必須以存儲用戶在註冊表單中輸入到一個XML文檔的詳細信息。

我設法做到了這一點,但問題是當新用戶註冊時,舊細節會覆蓋新用戶的詳細信息。因此,最終在XML文檔中只有1個用戶詳細信息。

我想知道是否有任何方法來保存新用戶的細節,同時「保留」舊的細節。

任何幫助表示讚賞:)

HTML代碼 -

<form id="rego" name="rego" method="get" action="register2.php"> 
<table width="719" border="0"> 
<tr> 
<td><div align="right">Email Address</div></td> 
    <td><label for="email"></label> 
    <input type="text" name="email" id="email" maxlength="50" /></td> 
</tr> 
    <td width="376"><div align="right">First Name</div></td> 
    <td width="333"><label for="firstName"></label> 
    <input type="text" name="firstName" id="firstName" maxlength="15" /></td> 
    <td><div id="underInput"></div></td> 
</tr> 
<tr> 
    <td><div align="right">Last Name</div></td> 
    <td><label for="lastName"></label> 
    <input type="text" name="lastName" id="lastName" maxlength="20" /></td> 
</tr> 
<tr> 
    <td><div align="right">Phone Number</div></td> 
    <td><label for="phoneNumber"></label> 
    <input type="text" name="phoneNumber" id="phoneNumber" maxlength="10" /></td> 
</tr> 
<tr> 

<tr> 
    <td><div align="right">Password</div></td> 
    <td><label for="password"></label> 
    <input type="password" name="password" id="password" maxlength="30" /></td> 
</tr> 
<tr> 
    <td><div align="right">Re-type password</div></td> 
    <td><label for="confirmPassword"></label> 
    <input type="password" name="confirmPassword" id="confirmPassword" maxlength="30" /></td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
</tr> 
<tr> 
    <td>&nbsp;</td> 
    <td><input type="Submit" name="Submit" id="Submit" value="Submit"/><!--onclick="saveForm();"--></td> 
</tr> 

PHP代碼(我使用DOM) -

<?php 
    $CustomerEmail = $_GET["email"]; 
    $CustomerFName = $_GET["firstName"]; 
    $CustomerLName = $_GET["lastName"]; 
    $CustomerPhoneNumber = $_GET["phoneNumber"]; 
    $CustomerPassword = $_GET["password"]; 
    $CustomerConfirmPassword = $_GET["confirmPassword"]; 


    $doc = new DomDocument('1.0'); 

    $root = $doc->createElement('customers'); 
    $root = $doc->appendChild($root); 

    $customer = $doc->createElement('customer'); 
    $customer = $root->appendChild($customer); 

    $email = $doc->createElement('email'); 
    $email = $customer->appendChild($email); 
    $valueEmail = $doc->createTextNode($CustomerEmail); 
    $valueEmail = $email->appendChild($valueEmail); 

    $fName = $doc->createElement('firstname'); 
    $fName = $customer->appendChild($fName); 
    $valueFName = $doc->createTextNode($CustomerFName); 
    $valueFName = $fName->appendChild($valueFName); 

    $lName = $doc->createElement('lastname'); 
    $lName = $customer->appendChild($lName); 
    $valueLName = $doc->createTextNode($CustomerLName); 
    $valueLName = $lName->appendChild($valueLName); 

    $phone = $doc->createElement('phone'); 
    $phone = $customer->appendChild($phone); 
    $valuePhone = $doc->createTextNode($CustomerPhoneNumber); 
    $valuePhone = $phone->appendChild($valuePhone); 

    $password = $doc->createElement('password'); 
    $password = $customer->appendChild($password); 
    $valuePassword = $doc->createTextNode($CustomerPassword); 
    $valuePassword = $password->appendChild($valuePassword); 

    $confirmPassword = $doc->createElement('confirmpassword'); 
    $confirmPassword = $customer->appendChild($confirmPassword); 
    $valueConfirmPassword = $doc->createTextNode($CustomerConfirmPassword); 
    $valueConfirmPassword = $confirmPassword->appendChild($valueConfirmPassword); 


    $doc->save('customer2.xml'); 
    ?> 

我任何道歉不便,敬請。

+1

您可以隨時將數據附加到xml。但是,您能否提供您正在使用的技術操作xml的詳細信息?發佈一些代碼也將是greate援助 – melc

+0

我正在使用GET方法從HTML中取文本框的值並將它們傳遞給PHP文件,該文件將值作爲子項添加到每個標記。這都是簡單的東西。 對不起,但我認爲這裏不需要代碼。如果你仍然想要,我可以發佈一些代碼。 謝謝:) – callMeJava

+0

需要更多細節。我們需要查看代碼,以便我們幫助解決問題。 – jrock2004

回答

0

您需要加載您的現有文件,然後每次創建一個新文件並覆蓋以前的文件。

..... 
    $doc = new DomDocument('1.0'); 

    if($xml = file_get_contents('customer2.xml')){ 
     $doc->loadXML($xml, LIBXML_NOBLANKS); 
     $root=$doc->getElementsByTagName('customers')->item(0); 
    }else{ 
     $root = $doc->createElement('customers'); 
     $root = $doc->appendChild($root); 
    } 

    $customer = $doc->createElement('customer'); 
    $customer = $root->appendChild($customer); 
    ..... 
    $doc->save('customer2.xml'); 
+0

它工作完美!非常感謝你,親切的先生。 對於之前造成的任何不便,我們深表歉意。 – callMeJava