Page 1 of 1

Saving HTML Form Data into an XML file using PHP

Posted: Fri Sep 17, 2004 5:36 am
by r_ward_2002
Firstly, I am new to this site and so i hope i am putting this in the right place. Apologies if not.

Secondly i should say i am only just beginning to learn php so a lot of functions still mean little to me.

My problem is:

I am trying to create a guest book / message board for my website. It will only be used by a few people so traffic will not be high. I have created an HTML form where name and message can be put in. There is then an xml file which the form data is supposed to get stored in. I am trying to write the php script that will take the data from the form and put it into the xml file. This is what i have tried:

Code: Select all

<?php 
$xml_string = 
"<guest> 
<name>$name</name>
<message>$message</message>
</guest>
"; 
$pagename = "guest.xml"; 
$fp=fopen("/inet/www.richard-ward.co.uk/html/".$pagename, "a+"); 
fwrite($fp, $xml_string); 
fclose($fp); 
print("Data sent to guest.xml"); 
?>
The problem with this is that that i end up with more than one (sorry don't know correct terminology) "primary" tag, so the file cannot be opened. I therefore assume i need to be able to add eg. <guestbook> </guestbook> at the start and end of the file. In which case - how do i write the information inside these tags.

Many thanks

Posted: Fri Sep 17, 2004 5:51 am
by patrikG
The cleanest way to generate XML on the fly is, arguably, using PHP's DOM XML extension (see:
http://www.phppatterns.com/index.php/ar ... cleview/38 ). I'd suggest to read up on how the whole thing works in practice - there is a good article introducing XML with PHP here and a more advanced one here.

Posted: Fri Sep 17, 2004 10:37 am
by r_ward_2002
Ok, Have read all three articles which has helped my general understanding, but am no nearer being able to write the code. From what i understand not all servers will allow DOM extensions (which i am slightly confused about too). What do you mean by "on the fly"?

Sorry!

Many thanks

Posted: Fri Sep 17, 2004 1:01 pm
by patrikG
r_ward_2002 wrote:Ok, Have read all three articles which has helped my general understanding, but am no nearer being able to write the code.
I would think you are much closer to writing the code compared to when you started. Getting the general idea is half-way towards the solution.
From what i understand not all servers will allow DOM extensions (which i am slightly confused about too).
You'll have to enable it when you install PHP. If you don't know about that, check your PHP setup with phpinfo(). If it is installed it should say DOM XML somewhere halfway down the document.

Regarding the code: one of the links I posted (on page three of http://www.sitepoint.com/article/manage ... stem-php/3 ), you'll find this bit of code:

Code: Select all

<?php
//create document root
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("article");
$root = $doc->append_child($root);
?>
which is exactly the beginning you want. It creates an XML document, and creates one element to it, which has a child element. The examples somewhat further down that article (page 3) should shed enough light on how to tackle this.

Personally, I couldn't find any good tutorials on XML and PHP when I got into it and I haven't played with it for about half a year now. Do check up the DOM functions in the manual, there are some good hints and clues in there.