This page contains the following errors: error in row 1 in column 1: document is empty

I could not find a 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>

I could not find a solution, please help. Below is the code. thanks in advance

+4
source share
4 answers

You need to wrap <form>in parts elseand remove the noeccesary statement echo.

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

<Person>
<name>Nameasdasd</name>
<LastName>LNameasdasd</LastName>
<Number>Number32424</Number>
</Person>
+3
source

You cannot mix XML and HTML in this way. This invalidates the XML document.

  • Delete

    echo $name.$Lname.$num; 
    

    Ref. Prologue. This is what causes the error you see.

  • Cancel the script according to the form (or use the else clause):

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

    Otherwise, you will get the data in the trailing section of the XML document.

HTML . , , enctype.

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

replace it with

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

Before writing or printing your xml, just clear the buffer in asp, for example: Response.Clear (); I do not know its equivalent in php.

0
source

Source: https://habr.com/ru/post/1530727/


All Articles