Monday 15 July 2013

php - dom cant use variables to store data in xml -



php - dom cant use variables to store data in xml -

i got error when trying post user input info html php , saved xml file. gives me error catchable fatal error: argument 1 passed domnode::appendchild() must instance of domnode, null given. here code

register.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>

relevant parts of register.php (whole code long)

$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 alter 'brian' works, doesnt work $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!'; }

just error suggests, appendchild needs domnode. create element, utilize sec paramter user input. example:

$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!'; }

php html xml domdocument

No comments:

Post a Comment