Jump to content

php & xml


runei

Recommended Posts

Hi people.

I've made a simple form for user where they can reply to a text. However i dont want to store the replies in a db, i want to store them in txt files. I have copied a couple of examples and tried to modify them but i dont work much with xml.

 

Heres the form:

<?php
		echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
		echo "<form method=\"post\" action=\"".$_SERVER['php_self']."\">\n";
		echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n";
		echo "<tr><td>Message</td><td><textarea id=\"djtextarea\" name=\"message\"></textarea></td></tr>";
		echo "<tr><td  align=\"right\"id=\"buttonlayout2\"><input id=\"buttonlayout1\" type=\"submit\"  name=\"submit\" value=\"Post\"></td></tr>\n";
		echo "</form></table>\n";

$username = trim(stripslashes(htmlspecialchars(utf8_decode($_POST['username']))));			
$message = trim(stripslashes(htmlspecialchars(utf8_decode($_POST['message']))));
?>

 

So i tried with the following xml expression from ibm.com but naturally it only stores one over the other, so i i only get one reply. Same thing if i save it as a string. How can i modify this one to store multiple inputs from the form?

 

<?php
$books = array();
  $books [] = array(
  'author' => "$username",
  'publisher' => "$message"
  );

  
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $r = $doc->createElement( "books" );
  $doc->appendChild( $r );
  
  foreach( $books as $book )
  {
  $b = $doc->createElement( "book" );
  
  $author = $doc->createElement( "author" );
  $author->appendChild(
  $doc->createTextNode( $book['author'] )
  );
  $b->appendChild( $author );
  
  $publisher = $doc->createElement( "publisher" );
  $publisher->appendChild(
  $doc->createTextNode( $book['publisher'] )
  );
  $b->appendChild( $publisher );
  
  $r->appendChild( $b );
  }
  
  echo $doc->save("reviews.xml");

?>

I get one xml file like so :

 

<?xml version="1.0"?>

<books>

  <book>

    <author>www</author>

    <publisher>wwww1111</publisher>

  </book>

</books>

 

 

 

 

 

I have tried the following one as well but i only get "

Warning: DOMDocument::load() [domdocument.load]: Extra content at the end of the document in file". Which means that my xml file is not well formed, but how do u get a well formed xml file when i am populating it directly through the form?

 

<?php
$myFile = "reviews.xml";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "<username>$username</username><message>$message</message>\n";
fwrite($fh, $stringData);
#$stringData = "<root><message>$message</message></root>\n";
#fwrite($fh, $stringData);
fclose($fh);
?>

Link to comment
https://forums.phpfreaks.com/topic/150353-php-xml/
Share on other sites

<?php
$books = array();
  $books [] = array(
  'author' => "$username",
  'publisher' => "$message"
  );

  
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $r = $doc->createElement( "books" );
  $doc->appendChild( $r );
  
  foreach( $books as $book )
  {
  $b = $doc->createElement( "book" );
  
  $author = $doc->createElement( "author" );
  $author->appendChild(
  $doc->createTextNode( $book['author'] )
  );
  $b->appendChild( $author );
  
  $publisher = $doc->createElement( "publisher" );
  $publisher->appendChild(
  $doc->createTextNode( $book['publisher'] )
  );
  $b->appendChild( $publisher );
  
  $r->appendChild( $b );
  }
  
  echo $doc->save("reviews.xml");

?>

 

you are redefining the root element everytime...

this is why you overwrite your file...

 

first, you should check if your file exists if not, then the code above is ok...

 

else, you should load it

$doc->load("yourXmlFile.xml");

 

go into the root element

$roots = $doc->getElementsById('books');
$root = $roots->item(0);

 

and then finaly append a new book to it

 

thus...

I guess the best thing you could do is write two functions

 

createXmlLog --> to create the XML file if it does not exist

appendNewBook --> to append a new book to the XML file

Link to comment
https://forums.phpfreaks.com/topic/150353-php-xml/#findComment-789645
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.