0

Pretty much in the title. I want to make a solution that saves the input data of a html form and display the entire csv file in a separate page that isn't connected. Here's the code I'm currently using that isn't saving the input data nor display the csv file. Input Data Page:

<!DOCTYPE HTML>  
<html>
<head>
</head>
<body>

<h2>PHP Form Validation Example</h2>
<form method="post" name="test1" action="" onsubmit="required()">  
 Name: <input type="text" name="name">

<br><br>
 E-mail: <input type="text" name="email">

<br><br>
Website: <input type="text" name="website">

<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>

<br><br>
<input type="submit" name="submit" value="Submit">  
</form>
</body>
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['website'];
$comment = $_POST['comment'];

$csv = 
array($_POST['name'],$_POST['email'],$_POST['website'],$_POST['comment']);

$file = fopen('form.csv', 'a');

foreach ($csv as $line) 
{
  fputcsv($file,explode(',',$line));
}

fclose($file); 
?>
</html>

PHP:Display CSV file

<!DOCTYPE html>
<html>

<?php
echo "</br >";
echo '<table style="width=100%">';

 // Print other student details here (minus the private stuff)
//The CSV file is read and printed on the page when using the 'r' value. 

$myfile = fopen("form.csv" , "r") or die ("Unable to open file");
while(!feof($myfile)) {
echo fgets($myfile) . "<br>"; 
}
fclose($myfile);

echo "</table>";
?>
<body>

</body>
</html>
TheRCMKid
  • 13
  • 3
  • `$csv = ` is overwriting `$csv` which remains a scalar variable use `$csv[] =` to make an array out of `$csv` – RiggsFolly Jun 08 '17 at 13:46
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Jun 08 '17 at 13:47
  • Welcome to stack overflow. As a rule of thumb the question should be, well a question, and we shouldn't have to read the body to figure out what you are asking. In your case, I'd recommend changing the question to "How do I save input data in to a CSV file, then display it in another page?" Also note that putting 'PHP' in the question is unnecessary and frowned upon. Instead, just rely on the tags. Stack overflow will take care of the rest for you. :) https://stackoverflow.com/help/how-to-ask – Native Coder Jun 08 '17 at 14:34

0 Answers0