【问题标题】:dom cant use variables to store data in xmldom 不能使用变量在 xml 中存储数据
【发布时间】:2014-10-18 12:03:18
【问题描述】:

尝试将用户输入数据从 html 发布到 php 并保存到 xml 文件时出现错误。它给了我这个错误可捕获的致命错误:传递给 DOMNode::appendChild() 的参数 1 必须是 DOMNode 的实例,给定 null。这是代码

注册.htm

<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<form id="regform" method="post" action="register.php">
<fieldset id="person"> 
<legend>Your details:</legend>
<p><label for="fname">First Name:</label><input type="text" name="fname"/></p>
<p><label for="lname">Last Name:</label><input type="text" name="lname"/></p>
<p><label for="password">Password:</label><input type="password" name="password"/></p>
<p><label for="cpassword">Confirm Password:</label><input type="password" name="cpassword"/></p>            
<p><label for="email">Email:</label><input type="email" id="email" name="email"/></p>
<p><label for="phone">Phone:</label><input type="text" name="phone"/></p>
<input type="submit" value="Register"/>
</fieldset>
</form>
</body>
</html>

register.php相关部分(整个代码太长)

$fname = @trim($_POST["fname"]);
$lname = @trim($_POST["lname"]);
$password = @trim($_POST["password"]);
$cpassword = @trim($_POST["cpassword"]);
$phone = @trim($_POST["phone"]);
$email = @trim($_POST["email"]);
if(file_exists('../../customer.xml'))
{
    $xml2 = file_get_contents('../../data/customer.xml');
}

if(!file_exists('../../customer.xml'))
        {
            $dir = "../../data";
            $f = fopen("$dir/customer.xml", "w");
            $doc = new DOMDocument('1.0');
            $doc->formatOutput = true;
            $root = $doc->createElement('customer');
            $doc->appendChild($root);
            $user = $doc->createElement('user');
            $root->appendChild($user);
            $fname = $doc->createElement('fname');
            @override - if I change this to 'brian' then it works, doesnt work with $variable
            $fnameValue = $doc->createTextNode($fname);
            $fname->appendChild($fnameValue);
            $user->appendChild($fname);
            $lname = $doc->createElement('lname');
            $lnameValue = $doc->createTextNode($lname);
            $lname->appendChild($lnameValue);
            $user->appendChild($lname);
            echo $doc->save('../../data/customer.xml');
            //$doc->load('customer.xml');
            echo ' Registration Successful!';
        }

【问题讨论】:

    标签: php html xml domdocument


    【解决方案1】:

    就像错误提示的那样,appendChild 需要一个 DOMNode。只需创建该元素,然后使用用户输入中的第二个参数。示例:

    $fname = @trim($_POST["fname"]);
    $lname = @trim($_POST["lname"]);
    $password = @trim($_POST["password"]);
    $cpassword = @trim($_POST["cpassword"]);
    $phone = @trim($_POST["phone"]);
    $email = @trim($_POST["email"]);
    
    if(file_exists('../../customer.xml')) {
        $xml2 = file_get_contents('../../data/customer.xml');
    }
    
    if(!file_exists('../../customer.xml')) {
        $dir = "../../data";
        $f = fopen("$dir/customer.xml", "w");
        $doc = new DOMDocument('1.0');
        $doc->formatOutput = true;
    
        $root = $doc->createElement('customer');
        $doc->appendChild($root);
    
        $user = $doc->createElement('user');
        $root->appendChild($user);
    
        $fname_node = $doc->createElement('fname', $fname);
        $user->appendChild($fname_node);
    
        $lname_node = $doc->createElement('lname', $lname);
        $user->appendChild($lname_node);
    
        echo $doc->save('../../data/customer.xml');
        //$doc->load('customer.xml');
        echo ' Registration Successful!';
    }
    

    【讨论】:

      【解决方案2】:

      您正在重新使用$fname 变量。这就是你得到错误的原因。这是一个简单的印刷错误。您可以做的一件事是在输入变量前面加上 input,例如$input_fname 所以很明显你是从那里获得它们的。或者甚至创建一个像 $input = new stdClass; 这样的标准类,然后将 $_POST 变量分配给它:$input-&gt;fname = @trim($_POST["fname"]); - 这样就很清楚它代表什么,您也可以更轻松地传递输入。

      例子:

      $input = new stdClass;
      
      $input->fname     = @trim($_POST["fname"]);
      $input->lname     = @trim($_POST["lname"]);
      $input->password  = @trim($_POST["password"]);
      $input->cpassword = @trim($_POST["cpassword"]);
      $input->phone     = @trim($_POST["phone"]);
      $input->email     = @trim($_POST["email"]);
      
      $doc = new DOMDocument('1.0');
      $doc->formatOutput = true;
      
      $root = $doc->createElement('customer');
      $root = $doc->appendChild($root);
      foreach ($input as $key => $value) {
          $element = $doc->createElement($key, $value);
          $root->appendChild($element);
      }
      
      $doc->save('php://output');
      

      示例输出:

      <?xml version="1.0"?>
      <customer>
        <fname>Waltraud</fname>
        <lname>Van H&#xC3;&#xB6;mpenstetten</lname>
        <password></password>
        <cpassword></cpassword>
        <phone></phone>
        <email></email>
      </customer>
      

      在线演示:https://eval.in/private/29c08d9fafec22

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-27
        • 2012-12-27
        • 1970-01-01
        • 2020-01-21
        • 2018-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多