Forum Moderators: coopster
like this
form
<form>
<input name="a1" type="text" />
<input name="b2" type="text" />
<input name="c3" type="text" />
<input name="d4" type="button" />
</form>
<div>
<div>a1</div>
<div>b2</div>
<div>c3</div>
</div>
Welcome to Webmaster World. :)
Have you thought about giving this a try yourself? You will need PHP`s fopen, fwrite and fclose functions.
So, something like:
<?php//Directory to write to..
$dir = 'files/';if (isset($_POST['d4']))
{
$string = "<div>\n";
$string .= "<div>".$_POST['a1']."</div>\n";
$string .= "<div>".$_POST['b2']."</div>\n";
$string .= "<div>".$_POST['c3']."</div>\n";
$string .= "</div>";$fp = fopen($dir.'/data.txt', 'ab');
if ($fp)
{
fwrite($fp,$string);
fclose($fp);echo 'Data written ok..<br><br>';
}
}<form method="POST" action="index.php">
<input name="a1" type="text" />
<input name="b2" type="text" />
<input name="c3" type="text" />
<input name="d4" type="button" />
</form>
<form method="POST" action="index.php">
<input name="a1" type="text" value="a1" />
<input name="b2" type="text" value="b2" />
<input name="c3" type="text" value="c3" />
<input name="d4" type="button" value="d4" />
</form><?php
if (isset($_POST['d4']))
{
$string = "<div>\n";
$string .= "<div>".$_POST['a1']."</div>\n";
$string .= "<div>".$_POST['b2']."</div>\n";
$string .= "<div>".$_POST['c3']."</div>\n";
$string .= "</div>";
$fp = fopen('txt.txt', 'ab');
if ($fp)
{
fwrite($fp,$string);
fclose($fp);
echo 'Data written ok..';
}
}
?>
<umm>
I had a suspicion that we would come back to this question :) In a nutshell, I don't think so. If you look at the attributes for fopen or fwrite, you'll see that the file is typically opened with the file pointer set at the end of the file (for append) or at the beginning of the file (for write), and in the latter case you are overwriting the existing file.
But it CAN be done. It's not a single step, however. What I would do is read the existing file, possibly into an array. Then write the new entries, followed by writing the previously read data from the file. My logic for this would look something like:
a) read file
b) store data somewhere (array would work)
c) write new data (essentially creating a new file)
d) write the stored data