Edit XML with Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acondiff
Forum Newbie
Posts: 3
Joined: Tue Jun 15, 2010 12:24 pm

Edit XML with Form

Post by acondiff »

I am designing a portfolio website for a photographer and I wanted to create an admin system for him to edit the flash website.

I am loading a XML file into Flash. I want to be able to edit this xml data with PHP. I managed to figure out how to create a form that is generated from my xml file, but I need it to be functional.

Form: http://condiffphotography.com/xmledit.php
XML: http://condiffphotography.com/pricing.xml

Here is my code:

Code: Select all

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
	<h2>Edit Pricing</h2>
<form action="submit.php" method="post">
<div class="content">
<?php
  $doc = new DOMDocument();
  $doc->load( 'pricing.xml' );
  
  $pricing = $doc->getElementsByTagName( "package" );
  foreach( $pricing as $package )
  {  
  $titles = $package->getElementsByTagName( "title" );
  $title = $titles->item(0)->nodeValue;
  
  $prices = $package->getElementsByTagName( "price" );
  $price = $prices->item(0)->nodeValue;
  
  $descriptions = $package->getElementsByTagName( "description" );
  $description = $descriptions->item(0)->nodeValue;

  echo "
  <div id=\"package\" style=\"padding:20px\">
  Package:<br />
  Title: <input type=\"text\" value=\"$title\" name=\"title\" />
  <br />
  Price: <input type=\"text\" value=\"$price\" name=\"price\" />
  <br />
  Description: <br />
  <textarea name=\"description\" />$description</textarea><br />
  <input type=\"button\" name=\"remove\" value=\"Remove Package\"><br />
  </div> 
  ";
  }
  ?>
 
  
  </div>
  <input type="button" name="add" value="Add Package"><br />
  <input type="submit" name="mysubmit" value="Click">  
  </form>
<script>
 function addPackage(){ 
  $(".content").append('<div id="package" style="padding:20px">Package:<br />Title: <input type="text" value="" name="title" /><br />Price: <input type="text" value="" name="price" /><br />Description: <br /><textarea name="description" /></textarea><br /><input type="button" name="remove" value="Remove Package"><br /></div>');
  }
</script>
</body>
</html>
So basically I want to re-write the xml data based on the form values.

Is this difficult to do? Any help would be greatly appreciated.

Thanks.
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Edit XML with Form

Post by hypedupdawg »

I don't want to try and tell you how to design your website, but a MySQL database would be much easier to update, edit and / or add entries to than XML. It also has the added advantage that you could then sort the "packages", e.g. a user might want to only see packages below $2000 in price.

MySQL is also generally free with your webserver, and if you haven't already tried it I think this would be a great opportunity for you.
acondiff
Forum Newbie
Posts: 3
Joined: Tue Jun 15, 2010 12:24 pm

Re: Edit XML with Form

Post by acondiff »

I don't really have too much experience with databases. This is actually going in the wordpress admin in its functions.php file so that the photographer can manage his portfolio website with his blog. So I could use the wordpress database and add a table to it. But I really don't know how to do this. Any help would be great!

I just need the form to update the data according to its contents. If its easier with a database thats great.
acondiff
Forum Newbie
Posts: 3
Joined: Tue Jun 15, 2010 12:24 pm

Re: Edit XML with Form

Post by acondiff »

This is the wordpress admin system created in the functions.php file that I was referring to:
Image

The only thing thats not functional is, it doesn't save the data entered. I can add, remove, and edit the packages on the page, but it won't save it to the xml file it loads from. By the way, it probably would be easier to load an xml file into Flash, but I don't do this on a regular basis. Anyways, if anyone can lend me a hand that would be great!
Post Reply