...

View Full Version : PHP writing form data to an XML file



georgemaier
11-09-2011, 09:59 PM
Right, I need a little assistance with a problem I have.

HTML page - with a text box and submit button (currently not linked to any action)
XML document - already containing a few items.

What I want to be able to do is have some PHP code that allows the submit button to take the text from the text box, throw it as a new item in the XML document (on top of previous items if possible to make it chronologically ordered)
so basically I want my XML to look like this:

<ITEM>
<CONTENT>What the latest visitor wrote</CONTENT>
</ITEM>
<ITEM>
<CONTENT>What the user before that wrote</CONTENT>
</ITEM>
and so on, and so forth, you get the picture....

But i'm a complete novice to PHP and have no idea where to even start with with so any help would be very much appreciated

Spookster
11-09-2011, 11:12 PM
Well the first thing you will want to look into is to determine what XML parsing libraries you have available on your web host for PHP. You can ask your web host or you can also get that information from the PHP Info page. Some hosts make this available in your account but if not you can create a PHP page with the following:



phpinfo();


and then go to that page using your browser and it will print out all the information related to your PHP setup.

As for how you would approach this you would want to process the form data so it is available for translating over into XML. Then you would want to read in your current XML file and then take the data you got from the form and translate that into XML tags/values and append to your XML data and then write everything back to file.

This is list of potential XML parsers you might have available:
http://www.php.net/manual/en/refs.xml.php

Depending on what you have you can use the functions in one of those to do everything from open and parse the file to adding tags, validating against a schema, etc.

mlseim
11-10-2011, 02:10 AM
If it were me, I would do it this way.

1) Create a database (like MySQL).
2) Each form submission goes into the database with a timestamp.
3) After the database is written to each time (INSERT), you run a PHP script
to create the XML file for your data. It can by called "posts.xml" or something
like that. Overwrite it each time with your new data.

This allows you (the admin person) to actually have other scripts to scrutinize
the database, hide or remove posts, or amend the data ... and again, after changes,
the XML file is once again rewritten. The other advantage to doing this, you can
create different XML files for different uses of the data. Perhaps offer an XML file
for only the last week of data? Or create a special RSS file for other users?

Spookster
11-10-2011, 02:13 AM
If it were me, I would do it this way.

1) Create a database (like MySQL).
2) Each form submission goes into the database with a timestamp.
3) After the database is written to each time (INSERT), you run a PHP script
to create the XML file for your data. It can by called "posts.xml" or something
like that. Overwrite it each time with your new data.

This allows you (the admin person) to actually have other scripts to scrutinize
the database, hide or remove posts, or amend the data ... and again, after changes,
the XML file is once again rewritten. The other advantage to doing this, you can
create different XML files for different uses of the data. Perhaps offer an XML file
for only the last week of data? Or create a special RSS file for other users?

I would concur with that but based on OPs thread in the XML forum they don't wish to use a database at this time.
http://www.codingforums.com/showthread.php?t=243330

georgemaier
11-10-2011, 07:57 AM
It says on my hosting PHP 5 if that helps?

Could I just use file_get_contents to get the content, throw it in a string
then

new string = "<Item>
<Content>" + $textboxcontent + "</Content>
</Item>" + original file content

then some other file command to write the file back containing the new string content?
Would that make sense?

And yes, at this stage I want to keep it really simple so I'm going to get an XML system functioning, then look at database possibilities

mlseim
11-10-2011, 12:33 PM
Can you give me an example of the real XML file that you are creating?

In your case, without a database, I would read the XML like, simplexml_load_string();
Then, rewrite it back. To me that seems easiest, and it would allow you to put
the information in any order. Also, you can add more tags, like a timestamp for
each entry ... that would be useful also.

I'm sure someone has some information on PHP XML Classes and a more efficient
way, but I'm not that advanced (or knowledgable) about XML Classes.

georgemaier
11-10-2011, 01:05 PM
The real XML file currently contains

<CATALOG>
<CD>
<TITLE>Me again</TITLE>
<ARTIST>Post</ARTIST>
</CD>

<CD>
<TITLE>Hello world</TITLE>
<Artist>Post</Artist>
</CD>

</CATALOG>

I think I could remove the catalog tags and it would still work. The weird tags are as a result of the java script user end that reads the xml to display it's result, I pulled an example script and haven't got round to changing the tags it uses yet, so at the moment i'm just running with these.

mlseim
11-10-2011, 01:31 PM
Those tags are not weird.
XML tags can be anything you want them to be.
The structure (nesting of tags) is important, and the opening and closing of tags.

I'll post an example in a bit.

georgemaier
11-10-2011, 04:11 PM
simplexml_load_string(); made the page explode,
file_get_contents() omitted much of the content, and obviously removed lines

if you know of a way to just allow me to basically go

write file ($path, $newcontent + $oldcontent)

then I will be a happy chap. It seems like a stupidly simple thing to want to do but I run into error after error. Everything I've tried so far seems to destroy anything less than, greater than bracketed and read only that outside of that. Restrictions of the string data type?

Urggghhhhhhh

mlseim
11-10-2011, 04:16 PM
George ...

Call your XML file "catalog.xml"

Then run the script below.
Make sure you enter your full URL in the script.

Run it, view your XML file (refresh to update cache).
You'll see it add a new item each time you run it.
And that new item appears at the top of your XML file.



<?php
// XML File - use the full URL to your XML file ...
$xml_url = "http://www.yoursite.com/catalog.xml";

// Get your new entry for wherever you get it ...
// I'm using these for testing...
$title= "Entry".date("h:i:s");
$artist = "my new artist";

// Grab the data using CURL
$data = get_url_contents($xml_url);
$xml = simplexml_load_string($data);

// Begin building the XML file
$xml_script = "<CATALOG>\n";

$xml_script .= "<CD>\n";
$xml_script .= "<TITLE>$title</TITLE>\n";
$xml_script .= "<ARTIST>$artist</ARTIST>\n";
$xml_script .= "</CD>\n";

// Add the rest of the original XML file
foreach($xml->children() as $child) {
$xml_script .= "<{$child->getName()}>\n";
foreach($child->children() as $subchild) {
$xml_script .= "<{$subchild->getName()}>$subchild</{$subchild->getName()}>\n";
}
$xml_script .= "</{$child->getName()}>\n";
}

// Finish the XML file
$xml_script .= "</CATALOG>\n";

// Resave the XML file (overwrite the old one)
$myFile = "catalog.xml";
$fh = fopen($myFile, 'w+') or die("can't open file");
fwrite($fh, "$xml_script");
fclose($fh);

echo "Done";

function get_url_contents($url){
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}

?>




.

georgemaier
11-10-2011, 04:53 PM
Words cannot even begin to express how much I love you.

Tried it and it worked perfectly!

I have so much gratitude! You saved me so much time and effort - you are amazing!

mlseim
11-10-2011, 06:35 PM
I'm glad it worked.
When (or if) you start using MySQL, you won't need to grab (or read) in the
XML file each time. You'll open the database, and build the XML directly from that.

But keep that little test script in your "code snippets" folder for use with other things.
It's the basis for reading XML files using CURL. Some webhosts no longer allow
file open or file get contents from external URL's ... such as my webhost, so I use
CURL to read external files. The "child", "children" thing is good to learn also.


.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum