Skip to content

Instantly share code, notes, and snippets.

@kshoufer
Created October 23, 2014 15:22
Show Gist options
  • Save kshoufer/a992f8760efe0f55be56 to your computer and use it in GitHub Desktop.
Save kshoufer/a992f8760efe0f55be56 to your computer and use it in GitHub Desktop.
<?php
if (isset($_POST) && isset($_POST['submit'])) {
//echo "post data";
} else {
unlink('data.json');
}
if(isset($_POST['submit'])) {
unset($_POST['submit']);
$file = "data.json";
$output;
$post = $_POST;
$json_string = json_encode($_POST,JSON_PRETTY_PRINT);
$data = array_values($post);
$headers = array_keys($post);
if($fp = fopen('data.json', 'a+')) {
$line = fgets($fp);
file_put_contents($file, $json_string, FILE_APPEND);
if (!$line == $headers) {
$output = "You Entered:<br />";
$output = $output. "<table>";
$output = $output. "<tr>";
$output = $output. "<th>";
$output = $output. $headers[0];
$output = $output. "</th>";
$output = $output. "<th>";
$output = $output. $headers[1];
$output = $output. "</th>";
$output = $output. "<th>";
$output = $output. $headers[2];
$output = $output. "</th>";
$output = $output. "</tr>";
$output = $output. "<tr>";
$output = $output. "<td>";
$output = $output. $data[0];
$output = $output. "</td>";
$output = $output. "<td>";
$output = $output. $data[1];
$output = $output. "</td>";
$output = $output. "<td>";
$output = $output. $data[2];
$output = $output. "</td>";
$output = $output. "<tr/>";
$output = $output. "<table/>";
echo $output;
} else {
$output = "You Entered:<br />";
$output = $output. "<table>";
$output = $output. "<tr>";
$output = $output. "<th>";
$output = $output. $headers[0];
$output = $output. "</th>";
$output = $output. "<th>";
$output = $output. $headers[1];
$output = $output. "</th>";
$output = $output. "<th>";
$output = $output. $headers[2];
$output = $output. "</th>";
$output = $output. "</tr>";
$output = $output. "<tr>";
$output = $output. "<td>";
$output = $output. $data[0];
$output = $output. "</td>";
$output = $output. "<td>";
$output = $output. $data[1];
$output = $output. "</td>";
$output = $output. "<td>";
$output = $output. $data[2];
$output = $output. "</td>";
$output = $output. "<tr/>";
$output = $output. "<table/>";
echo $output;
}
fclose($fp);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>form to csv file</title>
<link rel="stylesheet" href="style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function deleteFile()
{
if (!post)
$.ajax({
url: 'delete.php',
data: {file : 'data.json'},
success: function (response) {
//alert("data.json reset");
},
error: function () {
//alert("error!!");
}
});
}
</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">
<br />
<br />
<label for="email">Email: </label>
<input type="text" name="email" id="email">
<br />
<br />
<label for="phone">Phone: </label>
<input type="text" name="phone" id="phone">
<br />
<br />
<input type="submit" name="submit" id="submit" value="Submit">
<br />
<br />
</p>
</form>
<h3>Download JSON Link</h3>
<h4><a href="data.json">CLICK HERE</a></h4>
</body>
</html>
<?php
unlink($_GET['data.json']);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment