How to add new content in XML using php


In index.php add the following form for inserting new data in xml using php


<form action="booklistaction.php" method="post">
 <table>
   <tr> 
     <td colspan="2"class="labelcell"><label for="btitle">Book Title:</label></td>
     <td colspan="2"class="fieldcell"><input type="text" id="btitle" name="btitle"  tabindex="1"/></td>
   </tr>
      <tr> 
     <td colspan="2"class="labelcell"><label for="bdate">Book Issue date:</label></td>
     <td colspan="2"class="fieldcell"> <input type="text" id="bdate" name="bdate"  tabindex="2"/><br />
</td>
   </tr>
         <tr> 
     <td colspan="2"class="labelcell"><label for="bookrentdate">Book Rent date:</label></td>
     <td colspan="2"class="fieldcell"> <input type="text" id="bookrentdate" name="bookrentdate" tabindex="3"/> <br />
</td>
   </tr>
   <tr>
 <td colspan="2"class="labelcell"><label for="booksid">Student Name ID :</label></td>
    <td colspan="2"><input type="text" id="booksid" name="booksid" tabindex="4"></td></tr>
     </td>
   </tr>
   <tr><td colspan="4"><input type="submit" name="upload" class="box" value="Submit" tabindex="5" /></td></tr>
  </table>

</form>


Then create booklistaction.php and add the following code in this file:


<?php
$book = array(
    'bid' => $_POST['bid'],
    'btitle' => $_POST['btitle'],
    'bdate' => $_POST['bdate'],
    'bookrentdate' => $_POST['bookrentdate'],
    'booksid' => $_POST['booksid'],   
);
 
echo $book["bid"];
$doc = new DOMDocument();
$doc->load( 'loaned.xml' );


$booklist = $doc->getElementsByTagName( "book" );
foreach( $booklist as $book1 )
{
  $bookids = $book1->getElementsByTagName( "ID" );
  $bookid = $bookids->item(0)->nodeValue;  
}
echo "<b>$bookid<br>";
$bookid=$bookid+1;
$book["bid"] = $bookid;
echo "<b>$bookid<br>";


$doc->formatOutput = true;
$r = $doc->getElementsByTagName("resource")->item(0);

$b = $doc->createElement("book");

$bid = $doc->createElement("ID");
$bid->appendChild(
    $doc->createTextNode( $book["bid"] )
);
$b->appendChild( $bid );

$btitle = $doc->createElement("title");
$btitle->appendChild(
    $doc->createTextNode( $book["btitle"] )
);
$b->appendChild( $btitle );


$bdate = $doc->createElement("date");
$bdate->appendChild(
    $doc->createTextNode( $book["bdate"] )
);

$b->appendChild( $bdate );

$bookrentdate = $doc->createElement("retdate");
$bookrentdate->appendChild(
    $doc->createTextNode( $book["bookrentdate"] )
);
$b->appendChild( $bookrentdate );

$booksid = $doc->createElement("SID");
$booksid->appendChild(
    $doc->createTextNode( $book["booksid"] )
);

$b->appendChild( $booksid );
$r->appendChild( $b );
   
$doc->save("loaned.xml");

header("Location: {$_SERVER['HTTP_REFERER']}");   
?>
</body>

Comments

Popular posts from this blog

CSS Mouse Cursors and Custom Cursors

Block right click or disable keyboard on web pages

How to delete xml node using php