Jump to content

form data and text file


nubor

Recommended Posts

Ok..so I am learning php and having isues with form processing and text files. What I am trying to accomplish is:

  1. submitting user input
     
  2. saving this data to an array
     
  3. validating that the data has been submitted
     
  4. sanitizing the data
     
  5. saving the array to a .txt file
     
  6. each form submission would append the new data array to the .txt file on a new line

Then I am trying to create a page that:

  1. Pulls the data from the .txt file
     
  2. displays the data in a table on a webpage
     
  3. each line of the .txt file is a new array
     
  4. each array is a new row in the table.

Once I get my head around this then, I want to update certain data fields. However, that will be after I can get my head around the above first. Below is the code that I have so far.

 

Also, any insight on whether or not this is an appropriate and efficient method for validating and sanitizing would be helpful. I have read several articles and tutorials which have provided good information, but left gaps to be completely understood. Any assistance or insight would be appreciated. Thank You!

 

The all in one form

 

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8" />
<title>Software Bug</title>

</head>
<body>
<?php
if (isset($_POST['submit'])){
$bug["product"] =trim($_POST['product']);
$bug["version"] = trim($_POST['version']);
$bug["hardware"] = trim($_POST['hardware']);
$bug["os"] = trim($_POST['os']);
$bug["issue"] = trim($_POST['issue']);
/* check user input fields for values.
 * if empty or not set or null echo key for empty field
 */

foreach ($bug as $key=>$value){
 if (empty($value) || $value == "" || $value == NULL){
 echo $key . " is not set<br />";
 } else {echo $key . " is set<br />";}
}

/* create filter array to sanitize data */
$bugCleanser = array(
 "product" => FILTER_SANITIZE_STRING,
 "version" => FILTER_SANITIZE_ENCODED,
 "os" => FILTER_SANITIZE_STRING,
 "issue" =>FILTER_SANITIZE_STRING
 );
$cleanBugs = filter_var_array($bug, $bugCleanser);
var_dump($cleanBugs);

$file = fopen("solution.txt", "ab");	
fputcsv($file, $cleanBugs);
fclose($file);
}
?>

<form action="softwareform2.php" method="post">
<p>Product Name:<input type="text" name="product" value="" />
<p>Product Version:<input type="text" name="version" value="" />
<p>Hardware:<input type="text" name="hardware" value="" />
<p>OS:<input type="text" name="os" value="" />
<p>Issue:<br /><textarea rows="6'" cols="60" name="issue" value=""></textarea></p>
<p><input type="submit" name="submit" value="Enter" />  
 <input type="reset" name="reset" value="reset" /></p>
</form>
<a href="showbugs.php">Show Software Bugs</a>
</body>
</html>

 

This code to display the contents of the text file in a table. There are some commented out table tags because I had a somewhat working form, until I decided to a different method using array's

 

<?php
$solution = fopen("solution.txt", "r");
echo "<table width=\"80%\" border=\"1 solid black\">";
echo "<tr><th>Product Name</th><th>Product Version</th><th>Hardware</th><th>OS</th><th>Solution</th></tr>\n";
if ($solution){
while (!feof($solution)){
$lines[] = fgets($solution);	
}
fclose($solution);
foreach ($lines as $line){echo $line . "<br />";}
}

// for ($i=0; $i<count($solution); ++$i){
// $currSolution = explode(", ", $solution[$i]);
// //echo "<p>Solution " . ($i+1) . "<br />\n";
// echo "<tr>";
// echo "<td>{$currSolution[0]}</td>";
// echo "<td>{$currSolution[1]}</td>";
// echo "<td>{$currSolution[2]}</td>";
// echo "<td>{$currSolution[3]}</td>";
// echo "<td>{$currSolution[4]}</td>";
// echo "</td></tr>\n";
// }
// echo "</table>";
?>

Link to comment
Share on other sites

Thank you...good question.. First, I am still learning php and trying to get a grasp on working with directories and file systems. I know flat file systems are outdated where a database is preferred. However, I am trying to get a grasp on this before moving on to a DBMS for a solid foundation and understanding of working with php.

 

Likewise, I know that this is procedural codeing and once I get a handle on the basics of PHP, I will be moving on to learning OOP and MVC with PHP.

 

So, I guess this is part of my learning curve and if I can get a grasp on the PHP language, then I will start learning the different Frameworks.

 

I find this fun, interesting, and fustrating all at the same time. I have been reading books, forums, and online tutorials which seem to leave gaps in the information. Being a novice it can be difficult to disseminate this information. Also, with so many different ways to accomplish the same task, it can be difficult to understand best practices.

 

I have been following this forum for about a month and find it has helpful resources which why I joined. Thank you for your time and insight.

Link to comment
Share on other sites

Something like this should work

$solution = fopen("solution.txt", "r");
echo "<table width=\"80%\" border=\"1 solid black\">";
echo "<tr><th>Product Name</th><th>Product Version</th><th>Hardware</th><th>OS</th><th>Solution</th></tr>\n";
if ($solution) {
   while ($line = fgetcsv($solution, 1024)) {
    echo '<tr><td>' . join ('</td><td>', $line) . "</td></tr>\n";  
   }
   fclose($solution);
}
echo "/table>\n";

Link to comment
Share on other sites

Thanks alot...I did go back and used fputcsv and saved to a text file...and then used fgetcsv to retrieve the file...and it worked!! here is my code that retrieves the data and formats it as a table. Now what, I want to do is try to allow a user to update a field...not sure how to solve this one but I am going to try. Thanks for all your assistance.

 

<!DOCTYPE html>
<html>
<head>

  <meta charset="utf-8" />
  <title>Software Bug</title>

</head>
<body>
 <?php
  //Get Form data and assign it to an array  
  if (isset($_POST['submit'])){
   $bug["product"] =trim($_POST['product']);
   $bug["version"] = trim($_POST['version']);
   $bug["hardware"] = trim($_POST['hardware']);
   $bug["os"] = trim($_POST['os']);
   $bug["issue"] = trim($_POST['issue']);
   /* check user input fields for values.
 * if empty or not set or null echo key for empty field
 */   
   foreach ($bug as $key=>$value){
 if (empty($value) || $value == "" || $value == NULL){
 echo $key . " is not set<br />";
 } else {echo $key . " is set<br />";}
   }   
   /* create filter array to sanitize data */
   $bugCleanser = array(
  "product" => FILTER_SANITIZE_STRING,
  "version" => FILTER_SANITIZE_ENCODED,
  "hardware" => FILTER_SANITIZE_STRING,
  "os" => FILTER_SANITIZE_STRING,
  "issue" =>FILTER_SANITIZE_STRING
  );

   //assign sanitized data to a new array
   $cleanBugs = filter_var_array($bug, $bugCleanser);
   //write array data to a tab delimited text file
   $file = fopen("solution.txt", "a");
   fputcsv($file, $cleanBugs, "\t");
   fclose($file);


  }
 ?>

 <form action="softwareform2.php" method="post">
  <p>Product Name:<input type="text" name="product" value="" />
  <p>Product Version:<input type="text" name="version" value="" />
  <p>Hardware:<input type="text" name="hardware" value="" />
  <p>OS:<input type="text" name="os" value="" />
  <p>Issue:<br /><textarea rows="6'" cols="60" name="issue"  value=""></textarea></p>
  <p><input type="submit" name="submit" value="Enter" />  
 <input type="reset" name="reset" value="reset" /></p>
 </form>
 <a href="showbugs.php">Show Software Bugs</a>
</body>
</html>

 

<?php

echo "<table width=\"80%\" border=\"1 solid black\">\n";
echo "<tr><th>Product Name</th><th>Product Version</th><th>Hardware</th><th>OS</th><th>Solution</th></tr>\n";

$openFile = fopen("solution.txt", "r");
while (!feof($openFile)){
 $fileContents = fgetcsv($openFile, filesize("solution.txt"), "\t");
 echo "<tr>";
 for ($i=0; $i <count($fileContents); ++$i){  
  echo "<td>" . $fileContents[$i] . "</td>";  
 }
 echo "</tr>\n"; 
}
fclose($openFile);
echo "</table>";
?>

Link to comment
Share on other sites

Arguably the wrong way; at least for this day in age.

 

For clearification...the code above is the wrong way or working with plain text files. It is my understanding that XML are a form of a flat file Database, and one of the reasons why I am trying to grasp some of these concepts. I understand that today is a database driven enviroment...so should I just forget about working with flat files and move to concentrating on more database interaction?

 

Being that I am green to some of these concepts I appreciate the insight from more experienced developers. Thank you for your time and insights.

Edited by nubor
Link to comment
Share on other sites

If all you want to do do is store data, flat file is fine.

 

(As for XML I have a theory that someone at microshaft was instructed to buy up all the shares they could in companies selling storage devices as they were about to impose a technology that would increase storage requirements by a factor of ten.)

 

I digress.

 

If you want to do more with the data, such as update it, search it, sort it, then a database is the tool you should be using.

Link to comment
Share on other sites

While I agree 100% with Barand, I do think the flat-file concepts are very important. At some point in the future, you will have to read a flat-file to bring data into your database, or write a flat-file from data in the database (for someone else). So, don't give up on understanding the file system and flat-file use. And, as I understand your intent right now (to learn), even working to allow updates to that flat-file will benefit you in understanding how file operations behave.

Link to comment
Share on other sites

I sort of agree. Knowing how to work with files will likely be necessary at some point in time, but knowing how to use a flat file database is pretty much pointless these days. Once you hit any level of complexity that needs a JOIN, your flat file database becomes worthless. Time is better spent learning how to manipulate files, and how to use databases.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.