当试图将用户输入数据从html发布到php并保存到xml文件时出现错误。它给了我这个错误可捕获的致命错误:传递给DOMNode :: appendChild()的参数1必须是DOMNode的一个实例,null给出。下面是代码dom不能使用变量将数据存储在xml中
register.htm register.php的
<!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>
相关部分(整个代码过长)
$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!';
}