php 此页面包含以下错误:第 1 行第 1 列错误:文档为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22267116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:46:35  来源:igfitidea点击:

This page contains the following errors: error on line 1 at column 1: Document is empty

phpxml

提问by Muzammil

Im unable to find out the solution , please help. below is the code. Thanks in advance

我无法找到解决方案,请帮助。下面是代码。提前致谢

<?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>

Im unable to find out the solution , please help. below is the code. Thanks in advance

我无法找到解决方案,请帮助。下面是代码。提前致谢

采纳答案by Shankar Damodaran

You need to wrap the <form>in the elsepart and remove the unneccesary echostatement.

您需要将 包裹<form>else零件中并删除不必要的echo语句。

The working code..

工作代码..

<?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 :

OUTPUT :

<Person>
<name>Nameasdasd</name>
<LastName>LNameasdasd</LastName>
<Number>Number32424</Number>
</Person>

回答by user13500

You can not mix XML and HTML in that way. It invalidates the XML document.

您不能以这种方式混合 XML 和 HTML。它使 XML 文档无效。

  1. Remove:

    echo $name.$Lname.$num; 
    

    Ref. prolog.This is what causes the error you witness.

  2. Abort script on form data (or use an elseclause):

    print($xml->asXML());
    exit(0);
    

    Else you get data in trailing sectionof XML-document.

  1. 消除:

    echo $name.$Lname.$num; 
    

    参考 序言。这就是导致您目睹错误的原因。

  2. 中止表单数据脚本(或使用else子句):

    print($xml->asXML());
    exit(0);
    

    否则,您将在XML 文档的尾随部分获取数据。

For valid HTML you also need to add a value for actionin the form. Quotes on attribute values are usually optional, but would advice consistency, thus also quote enctype.

对于有效的 HTML,您还需要在表单中action添加一个。属性值的引用通常是可选的,但会建议一致性,因此也引用enctype

回答by Masoud

Before writing or printing your xml just clear your buffer in asp for example: Response.Clear(); I don't know its equivalent in php.

在编写或打印您的 xml 之前,只需清除 asp 中的缓冲区,例如:Response.Clear(); 我不知道它在 php 中的等价物。

回答by wild

<form method="post" action="" enctype= multipart/form-data>

replace it with

将其替换为

<form method="post" action="" enctype="multipart/form-data">