Last active
August 29, 2015 14:07
-
-
Save kshoufer/b201500ca258dee69a92 to your computer and use it in GitHub Desktop.
Form to CSV File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (isset($_POST) && isset($_POST['submit'])) { | |
//echo "post data"; | |
} else { | |
unlink('data.csv'); | |
} | |
if(isset($_POST['submit'])) { | |
unset($_POST['submit']); | |
$file = "data.csv"; | |
$output; | |
$post = $_POST; | |
$data = array_values($post); | |
$headers = array_keys($post); | |
if($fp = fopen('data.csv', 'a+')) { | |
$line = fgets($fp); | |
if (!$line == $headers) { | |
fputcsv($fp, $headers); | |
fputcsv($fp, $data); | |
$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 { | |
fputcsv($fp, $data); | |
$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.csv'}, | |
success: function (response) { | |
//alert("data.csv 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 CSV Link</h3> | |
<h4><a href="data.csv">CLICK HERE</a></h4> | |
<br /> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
unlink($_GET['data.csv']); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
td { | |
text-align: left; | |
padding-left: 20px; | |
} | |
th { | |
text-align: left; | |
padding-left: 20px; | |
} | |
table { | |
border-bottom: 3px solid #000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment