【问题标题】:Append text from HTML to XML将文本从 HTML 附加到 XML
【发布时间】:2013-10-19 12:42:17
【问题描述】:

生日,

作为我任务的一部分,我必须将用户在注册表单中输入的详细信息存储到 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');
    ?>

对于任何不便,我深表歉意。

【问题讨论】:

  • 您始终可以将数据附加到 xml。但是您能否提供您正在使用的技术的详细信息来操作您的 xml?发布一些代码也会有很大帮助
  • 我正在使用 GET 方法从 HTML 中获取文本框的值,并将它们传递给 PHP 文件,该文件将值作为子项附加到每个标签。这都是简单的东西。对不起,我认为这里不需要代码。如果你仍然想要,我可以发布一些代码。谢谢你:)
  • 需要更多细节。我们需要查看代码,以便帮助找出问题所在。
  • 很抱歉。我现在已经添加了代码。
  • @melc 代码起来了。

标签: html xml forms appendchild


【解决方案1】:

您需要加载现有文件,然后每次创建一个新文件并覆盖之前的文件。

    .....
    $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');

【讨论】:

  • 效果很好!非常感谢,好心的先生。对于之前造成的任何不便,我深表歉意。
猜你喜欢
  • 1970-01-01
  • 2018-12-20
  • 2011-05-24
  • 2020-05-01
  • 2020-12-07
  • 1970-01-01
  • 2014-09-03
  • 2011-02-17
  • 1970-01-01
相关资源
最近更新 更多