title: How to store php form data to a web server in a plain text file?
body: <p>I have an assignment that calls for data entries from a php form to be stored in a plain text file on the same web server. I created the php form page and the plain text file but I am unsure how to connect the two. I've searched the internet and tried multiple ways for about two hours now and no dice. The data entries have to accumulate in the plain text file too (1st person submits, and a 2nd person submitting can see 1st person's submission and so on).</p>
  
  <p>I didn't add any of the code I've tried to the plain text file because none of them were working and I wanted to (in theory) simplify the process of not having to try and fix the code. I know that the plain text file needs some sort of code to retrieve the code from the php form but unsure what to try at this point. And yes I wrote permissions for the plain text file to be writable via FileZilla.</p>
  
  <p>Here's my php form code:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt; 
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Roy Feedback Form Assignment 7&lt;/title&gt;
  &lt;style&gt;
  .error {color: #FF0000;}
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt; 
  
  &lt;?php
  // define variables and set to empty values
  $nameErr = $emailErr = $commentErr = $likesErr = $howErr = $rateErr = "";
  $name = $email = $comment = $likes = $how = $rate = "";
  
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
   $nameErr = "Name is required";
  } else {
   $name = test_input($_POST["name"]);
  }
  
  
  $email = test_input($_POST["email"]);
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format"; 
  }
  
  if (empty($_POST["comment"])) {
   $commentErr = "Comments are required";
  } else {
   $comment = test_input($_POST["comment"]);
  }
  
  if (empty($_POST["likes"])) {
   $likesErr = "Things you liked is required";
  } else {
   $likes = test_input($_POST["likes"]);
  }
  
  if (empty($_POST["how"])) {
   $howErr = "How you got to our site is required";
  } else {
   $how = test_input($_POST["how"]);
  }
  
   if (empty($_POST["rate"])) {
   $rateErr = "Rating our site is required";
  } else {
   $rate = test_input($_POST["rate"]);
  }
  }
  
  function resetForm($form) {
  $form.find('input:text, input:password, input:file, select, textarea').val('');
  $form.find('input:radio, input:checkbox')
       .removeAttr('checked').removeAttr('selected');
  }
  
  function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
  }
  ?&gt; 
  
  &lt;h2&gt;Roy Feedback Form Assignment 7&lt;/h2&gt;
  &lt;p&gt;&lt;span class="error"&gt;* required field.&lt;/span&gt;&lt;/p&gt;
  &lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?&gt;"&gt; 
  
  Name: &lt;input type="text" name="name"&gt;
  &lt;span class="error"&gt;* &lt;?php echo $nameErr;?&gt;&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;
  
  E-mail: &lt;input type="text" name="email"&gt;
  &lt;span class="error"&gt;* &lt;?php echo $emailErr;?&gt;&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;
  
  Comment: &lt;textarea name="comment" rows="5" cols="40"&gt;&lt;/textarea&gt;
  &lt;span class="error"&gt;* &lt;?php echo $commentErr;?&gt;&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;
  
  Things you liked:
  &lt;input type="radio" name="likes" value="Site design"&gt;Site design
  &lt;input type="radio" name="likes" value="Links"&gt;Links
  &lt;input type="radio" name="likes" value="Ease of use"&gt;Ease of use
  &lt;input type="radio" name="likes" value="Images"&gt;Images
  &lt;input type="radio" name="likes" value="Source code"&gt;Source code
  &lt;span class="error"&gt;* &lt;?php echo $likesErr;?&gt;&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;
  
  How you got to our site:
  &lt;input type="radio" name="how" value="Search engine"&gt;Search engine
  &lt;input type="radio" name="how" value="Links from another site"&gt;Links from another site
  &lt;input type="radio" name="how" value="Deitel.com website"&gt;Deitel.com website
  &lt;input type="radio" name="how" value="Reference from a book"&gt;Reference from a book
  &lt;input type="radio" name="how" value="Other"&gt;Other
  &lt;span class="error"&gt;* &lt;?php echo $howErr;?&gt;&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;
  
  Rate our site:
  &lt;select name="rate"&gt;
  &lt;option value=""&gt;- Please Select -&lt;/option&gt;
  &lt;option value="1"&gt;1&lt;/option&gt;
  &lt;option value="2"&gt;2&lt;/option&gt;
  &lt;option value="3"&gt;3&lt;/option&gt;
  &lt;option value="4"&gt;4&lt;/option&gt;
  &lt;option value="5"&gt;5&lt;/option&gt;
  &lt;option value="6"&gt;6&lt;/option&gt;
  &lt;option value="7"&gt;7&lt;/option&gt;
  &lt;option value="8"&gt;8&lt;/option&gt;
  &lt;option value="9"&gt;9&lt;/option&gt;
  &lt;option value="10"&gt;10&lt;/option&gt;
  &lt;/select&gt;
  &lt;span class="error"&gt;* &lt;?php echo $rateErr;?&gt;&lt;/span&gt;
  &lt;br/&gt;&lt;br/&gt;
  
  &lt;input type="submit" name="submit" value="Submit"&gt; 
  &lt;input type="reset" value="Reset"&gt;
  
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here's the plain text file code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Roy Feedback Results Assignment 7&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Update 1</strong>
  So I'm still having issues (sorry, I know it's like trying to teach PHP to a grapefruit). So when I use the following code nothing happens on the plain text page (as in the data isn't stored where I tell it to be):</p>
  
  <pre><code>function file_write($data, $feedback_results_html_only){
  if(is_readable($feedback_results_html_only)){
      if(is_string($data)){
          return file_put_contents($feedback_results_html_only, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time.
      }//return an error message if the data isnt a string
  }//return an error message if the file doesnt exist or isnt readable
  }
  </code></pre>
  
  <p>However when I use the following code it at least places the "John Smith" name into the file (which is the first time I actually got it to somewhat work, hooray coding!):</p>
  
  <pre><code>&lt;?php
  $file = 'feedback_results_html_only.php';
  // Open the file to get existing content
  $current = file_get_contents($file);
  // Append a new person to the file
  $current .= "John Smith\n";
  // Write the contents back to the file
  file_put_contents($file, $current);
  ?&gt;
  </code></pre>
  
  <p>Also I am aware that I didn't use the ".php" on the "feedback_results_html_only" in the first example of code (in update 1) because it created an error. Could I possibly try something like the second example (in update 1) instead to make it work?</p>
  
  <pre><code>$file = 'feedback_results_html_only.php'
  </code></pre>