我无法找到解决方案,请帮忙。下面是代码。提前致谢
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$Lname = $_POST['lastName'];
$num = $_POST['number'];
echo $name.$Lname.$num;
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><Person></Person>");
Header('Content-type:text/xml');
$name = $xml->addChild('name',$name);
$Lname = $xml->addChild('LastName',$Lname);
$Number = $xml->addChild('Number',$num);
print($xml->asXML());
}
?>
<!DOCTYPE html>
<head>
<title>XML</title>
</head>
<body>
<form method="post" action="" enctype= multipart/form-data>
<input type="text" name="name" value="Name" required/>
<input type="text" name="lastName" value="LName" required/>
<input type="text" name="number" value="Number" required/>
<input type="submit" value="send" name="submit"/>
</form>
</body>
</html>
我无法找到解决方案,请帮忙。下面是代码。提前致谢
最佳答案
您需要将<form>
包装在else
部分中,并删除不必要的echo
语句。
工作代码
<?php
if(isset($_POST['submit'])){
header('Content-type:text/xml');
$name = $_POST['name'];
$Lname = $_POST['lastName'];
$num = $_POST['number'];
//echo $name.$Lname.$num; //<---- Commented
$xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><Person></Person>");
$name = $xml->addChild('name',$name);
$Lname = $xml->addChild('LastName',$Lname);
$Number = $xml->addChild('Number',$num);
print($xml->asXML());
}
else
{
?>
<!DOCTYPE html>
<head>
<title>XML</title>
</head>
<body>
<form method="post" action="" enctype= multipart/form-data>
<input type="text" name="name" value="Name" required/>
<input type="text" name="lastName" value="LName" required/>
<input type="text" name="number" value="Number" required/>
<input type="submit" value="send" name="submit"/>
</form>
</body>
</html>
<?php } ?>
OUTPUT :
<Person>
<name>Nameasdasd</name>
<LastName>LNameasdasd</LastName>
<Number>Number32424</Number>
</Person>
关于php - 此页面包含以下错误: error on line 1 at column 1: Document is empty,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22267116/