I am attempting to save user input data from an HTML form into a CSV file. My code runs and displays the user input on the browser correctly but when I go to check my .csv file in my www folder (opening it in wordpad) it is blank and none of the submitted user data is being stored. I am not receving any clear cut error message, but I do notice my "else" statement runs when I click on the link to check for stored data in the browser.
49er, thanks for your help but the links you left did not answer my question, they were extremely general answers and I believe my variable are initialized. So what should I use instead of the "filter_input"?
HTML:
<form action="ch7_access.php" method="post">
<label for="first">First:</label><input type="text" name="first" id="first" /><br/>
<label for="last">Last:</label><input type="text" name="last" id="last" /><br/>
<label for="phone">Phone:</label><input type="text" name="phone" id="phone" /><br/>
<input type="submit" name = 'submit' value="Submit" />
</form>
<a href="ch7_access2.php">View Stored Information</a>
PHP
<?php
if(isset($_POST['submit']))
{
$fname = $_REQUEST['first'];
$lname = $_REQUEST['last'];
$phone = $_REQUEST['phone'];
//read the entered data from form
$fname = filter_input(INPUT_POST, "fname");
$lname = filter_input(INPUT_POST, "lname");
$phone = filter_input(INPUT_POST, "phone");
//generate output for text file
$output = $fname . "\t";
$output .= $lname . "\t";
$output .= $phone . "\n";
$fp = fopen("info.csv", "w");
//write to the file
fputcsv($fp, $output);
fclose($fp);
}
else
{
echo "empty submit, please enter your information";
}
?>
<a href="Lab7.php">Go back to store information</a>