Skip to content

Instantly share code, notes, and snippets.

@kshoufer
Created October 22, 2014 03:48
Show Gist options
  • Save kshoufer/ce36421699897e5f2e9d to your computer and use it in GitHub Desktop.
Save kshoufer/ce36421699897e5f2e9d to your computer and use it in GitHub Desktop.
<?php
if (isset($_POST) && isset($_POST['submit'])) {
//echo "post data";
} else {
unlink('data.xml');
$file = "data.xml";
if($fp = fopen('data.xml', 'a+')) {
$string = '<?xml version="1.0"?>' . "\n" .
'<students>' . "\n" . ' <!-- User response to follow -->' .
"\n" . '</students>';
fputs($fp,$string);
}
}
if (isset($_POST['submit'])) {
$file = "data.xml";
$userNode = 'student';
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->load($file);
//$doc->formatOutput = true;
$root = $doc->documentElement;
$post = $_POST;
unset($post['submit']);
$user = $doc->createElement($userNode);
$user = $root->appendChild($user);
foreach ($post as $key => $value) {
$node = $doc->createElement($key, $value);
$user->appendChild($node);
}
if($doc->save($file)) {
echo "<h2>Entry saved</h2>";
} else {
die("Problem saving file");
}
}
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<div id="container">
<form name="form1" method="post" action="">
<p>
<label for="name">Name: </label>
<input type="text" name="name" id="name" placeholder="Your full name">
</p>
<p>
<label for="name">Email: </label>
<input type="text" name="email" id="email">
</p>
<p>
<label for="name">Cell: </label>
<input type="text" name="cell" id="cell">
</p>
<p>
<label for="name">Date: </label>
<input type="text" name="date" id="date">
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
</form>
<br />
<h3>Download XML Link</h3>
<h4><a href="data.xml" target="_blank">CLICK HERE</a></h4>
<h3>XML Entries From "data.xml":</h3>
<textarea rows="4" cols="50">
<div id="file-to-xml">
</div>
</textarea>
</div><!-- end div container -->
<script type="text/javascript">
$(function () {
$.ajax({
type: "GET",
url: "data.xml",
dataType: "xml",
success: xmlParser
});
});
function xmlParser(xml) {
$(xml).find("student").each(function() {
$('#file-to-xml').append('<p>');
$('#file-to-xml').append('Name: ' + $(this).find("name").text());
$('#file-to-xml').append(' -- Email: ' + $(this).find("email").text());
$('#file-to-xml').append(' -- Cell: ' + $(this).find("cell").text());
$('#file-to-xml').append(' -- Date: ' + $(this).find("cell").text());
$('#file-to-xml').append('</p>');
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment