I have created a HTML form that passes data to Text file and text file saves data on the array.
I cannot figure out how to go about checking to see if value already exists before putting another input in the Array or text file.
Name: <input type="text" name="name" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="save" value="Submit" /><br />
<?php
$myArray = array();
if (isset($_POST['save']))
{
/*The file will be created in the same directory where the PHP code resides
*a+ to not overwrite text*/
$myfile = fopen("DonorList.txt", "a+") or die("Unable to open file!");
//get data entered by user from the form
fwrite($myfile, $_POST['name']);
fwrite($myfile, " ");
fwrite($myfile, $_POST['email']);
fwrite($myfile, "\r\n"); //next line
fclose($myfile);//close file
textOutput();//call function
}
print_r($myArray);
//creating function to make code more readable
function textOutput()
{
$lines = file('DonorList.txt');
foreach ($lines as $lines_num => $line)
{
echo "User Input: ".htmlspecialchars($line)."<br />\n";
}
$file = fopen("DonorList.txt", "r");
while(!feof($file))
{
$myArray[]= fgets($file);
}
fclose($file);
}
?>