0

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('my@email.com', $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
SaturnsEye
  • 6,297
  • 10
  • 46
  • 62
  • What you got? Any error? – bksi Aug 29 '14 at 09:35
  • I doubt you'll get any data at all with `$xml = new SimpleXMLElement('');` – l'L'l Aug 29 '14 at 09:36
  • @bksi Well i'm very new to any form of PHP as I've only ever really dealt with HTML and CSS so it's a bit of a guessing game for me. I don't seem to be getting any errors and my web form email just fine. But the XML file I have hosted doesn't update – SaturnsEye Aug 29 '14 at 09:36
  • @l'L'l I'm new to anything PHP and it was code someone else suggested. – SaturnsEye Aug 29 '14 at 09:37
  • @SaturnsEye, The string is empty and should be fairly obvious. – l'L'l Aug 29 '14 at 09:38
  • I think you're `display_errors` are turned off. May be helpful : [How to get useful error messages in PHP?](http://stackoverflow.com/a/845025/3361444) – Debflav Aug 29 '14 at 09:38
  • @l'L'l like I said, I'm new to anything PHP related so things like this go over my head. I've only ever dealt with HTML and CSS – SaturnsEye Aug 29 '14 at 09:39
  • Yes, understandable; a couple of things you can try - `$xml = new SimpleXMLElement($message);`. If you get back nothing try using `var_dump();` by adding it to the page to see which variables are defined. – l'L'l Aug 29 '14 at 09:43
  • @l'L'l added `$message` but still nothing. Is it something i'm doing wrong with the xml file itself? I have it stored in the same ftp location as the php file. – SaturnsEye Aug 29 '14 at 09:46
  • Do you see the file it updated/generated? and how about `var_dump();`, what did that display? – l'L'l Aug 29 '14 at 09:48
  • @l'L'l the file `VisitorData.xml` in my ftp doesn't seem to be affected at all. And no I havent tried `var_dump();` yet as I'm unsure exactly how to do it – SaturnsEye Aug 29 '14 at 09:50
  • http://php.net/manual/en/function.var-dump.php – l'L'l Aug 29 '14 at 10:01
  • @l'L'l I realised I was uploading the wrong file. Now that I've updated the correct file I'm now getting the errors that you can see in my updated quesrtion – SaturnsEye Aug 29 '14 at 10:22

1 Answers1

1

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

Debflav
  • 1,131
  • 7
  • 17
  • Excellent! it's finally getting somewhere. The XML data is updating to show the users name and IP but how can I get it so that it adds to the XML file and doesnt overwrite it every time? – SaturnsEye Aug 29 '14 at 11:13
  • 1
    If you want to add data to an existing xml file maybe this link can help you: [Insert node in xml using php](http://stackoverflow.com/q/1618509/3361444). – Debflav Aug 29 '14 at 11:21
  • Thank you so much! your suggested link works perfectly. I've also accepted your answer as being correct as it does want I wanted initially. – SaturnsEye Aug 29 '14 at 11:25