0

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>
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • 1
    `$fname = filter_input(INPUT_POST, "XXX");` will never work; they don't match (some of) the name attributes for the inputs. Error reporting would have caught that. – Funk Forty Niner Aug 08 '18 at 13:02
  • 1
    It seems logic error, correct way is : //read the entered data from form $fname = filter_input(INPUT_POST, "first"); $lname = filter_input(INPUT_POST, "last"); $phone = filter_input(INPUT_POST, "phone"); //generate output for text file $output[] = $fname; $output[] = $lname; $output[] = $phone; $fp = fopen("info.csv", "w"); fputcsv($fp, $output, "\t"); fclose($fp); – Yogesh Salvi Aug 08 '18 at 13:16
  • @Yogesh: thank you for your help I made those corrections: I made the corrections but for some reason the user input is still not appearing in my csv file when I submit the form. Any ideas? Current Code: – CodeConnoisseur Aug 08 '18 at 13:26
  • 1
    I tested your code, it seems to be working fine for me. you can try following things: - Remove old generated file and try to create new one. - Check form fields name, it has to be first, last and phone. – Yogesh Salvi Aug 08 '18 at 13:33
  • @YogeshSalvi it worked!!! You are awesome Yogesh! Upvote for sure. Also, thank you for your patience and understanding, I am new to PHP. – CodeConnoisseur Aug 08 '18 at 14:44

0 Answers0