PHP read text from file then fill fields

520

I've a page that shows some fields that can be saved to a file by the following code:

<?php

if($_POST['Submit'])
{
$company = $_POST['company'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$address4 = $_POST['address4'];
$contact = $_POST['contact'];
$contact2 = $_POST['contact2'];
$phone = $_POST['phone'];

$data = "$company \n $address1 \n $address2 \n $address3 \n $address4 \n $contact \n $contact2 \n $phone\n";

//open the file
$fh = fopen("/cfg/customers.txt", "a");
fwrite($fh, $data);

//close the file
fclose($fh);

print "Information Submitted. Thanks";
}
else
{
print <<<ENDOFTXT
</style><form name="form1" method="post" action="index.php">
<table width="780" border="0" align="center">
<tr>
<td width="256"><span >Company Name:</span></td>
<td width="514"><input name="company" type="text" id="company"></td>
</tr>

<tr>
<td><span >Address
:</span></td>
<td><input name="address1" type="text" id="address1" value=""></td>
</tr>
<tr>
<td><span >Unit / Apt :</span></td>
<td><input name="address2" type="text" id="address2"></td>
</tr>
<tr>
<td><span >City, Pr : </span></td>
<td><input name="address3" type="text" id="address3"></td>
</tr>
<tr>
<td><span >Postal Code :</span></td>
<td><input name="address4" type="text" id="address4"></td>
</tr>
<tr>
<td><span >Contact Name: </span></td>
<td><input name="contact" type="text" id="contact" value="First Name Last Name"></td>
</tr>
<tr>
<td><span >Email: </span></td>
<td><input name="contact2" type="text" id="contact2"></td>
</tr>
<tr>
<td><span >Phone :</span></td>
<td><input name="phone" type="text" id="phone" value="( ) ">
<input name="Submit" type="submit" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><div align="center"></div></td>
</tr>
</table>
<p align="center">&nbsp;</p>
</form>
ENDOFTXT;
}
?>

Now, I can read the text file using the follwoing code:

<?php
$lines = file("/cfg/customers.txt");
$top200 = array_slice(array_reverse($lines),0,200);
foreach($top200 as $line)
{
    echo $line . "<br />";
}

?>

But how can I put back to the original fields when the page loads for the first time? So, how can I merge the read routine to the above code?

People are also looking for solutions to the problem: php - Loop through array and display year once, months after

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.