SaturnsEye
SaturnsEye

Reputation: 6499

Save specific PHP data to XML

I can't seem to get specific data from my PHP to output to an XML file using my web form.

What am I doing wrong?

<?php

$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];

if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}

if(empty($visitor) || empty($visitormail) || empty($notes )) {
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! ");
}

$todayis = date("l, F j, Y, g:i a");

$attn = $attn ;
$subject = $attn;

$notes = stripcslashes($notes);

$message = "
Subject: $attn \n
Message: $notes \n 
From: $visitor ($visitormail)\n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
";

$from = "From: $visitormail\r\n";


mail('[email protected]', $subject, $message, $from);

?>

<?php

$xml = new SimpleXMLElement('');

    $mydata = $xml->addChild('VisitorInfo');
    $mydata->addChild('Visitor',$visitor);
    $mydata->addChild('Key',$ip);

    $mydata->PHP_EOL;

mysql_close($db);

$fp = fopen("VisitorData.xml","wb");

fwrite($fp,$xml->asXML());

fclose($fp);

?>

Error codes I'm receiving:

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 2: parser error : Start tag expected, '<' not found in /home/content/48/10101748/html/sendeail.php on line 57

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Subject: adfasdfd in /home/content/48/10101748/html/sendeail.php on line 57

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/content/48/10101748/html/sendeail.php on line 57

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/content/48/10101748/html/sendeail.php:57 Stack trace: #0 /home/content/48/10101748/html/sendeail.php(57): SimpleXMLElement->__construct('?Subject: adfas...') #1 {main} thrown in /home/content/48/10101748/html/sendeail.php on line 57

Upvotes: 0

Views: 107

Answers (1)

Debflav
Debflav

Reputation: 1151

From the PHP documentation of SimpleXMLElement the first value must be of type string and his data should be "A well-formed XML string or the path or URL to an XML document".

Currently, you don't fill these conditions. You should create a new node as for instance:

$xml = new SimpleXMLElement('<xml/>');
$mydata = $xml->addChild('VisitorInfo');
$mydata->addChild('Visitor','toto');
$mydata->addChild('Key', '1');

$mydata->PHP_EOL; // Don't understand the goal here

The output of this sample of code :

SimpleXMLElement Object
(
    [Visitor] => toto
    [Key] => 1
)

In addition, you are using mysql_* in your code. Think to switch to mysqli_* or PDO. From PHP documentation: "Warning: This extension is deprecated as of PHP5.5.0, and will be removed in the future. Instead, the MySQLi or PDO MySQL extension should be used."

Upvotes: 1

Related Questions