I have created a html form and a php which create and save a .txt file with the form input. It seams to be working fine, but how can I save the .txt file using the "Id" value given from the form. For instance if the user have an Id no. 1234 then the save .txt file should be 1234.txt (and not output.txt as in my code).
My form:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<form action="form.php" method="post">
<ol>
<li><label for="email">Email</label>
<input type="text" name="email" id="email"></li>
<li><label for="id">Id</label>
<input type="text" name="id" id="id"></li>
</ol>
</form>
</body>
</html>
and my php
<?php
ob_start(); // start trapping output
$id = @$_POST['id'];
$email = @$_POST['email'];
?>
<html>
<body>
<p>
Id: <?php echo $id; ?><br>
Email: <?php echo $email; ?>
</p>
</body>
</html>
<?php
$output = ob_get_contents(); // get contents of trapped output
//write to file, e.g.
$newfile="output.txt";
$file = fopen ($newfile, "w");
fwrite($file, $output);
fclose ($file);
ob_end_clean(); // discard trapped output and stop trapping
?>
Simply put it as the name of the file like:
<?php
$output = ob_get_contents();
//write to file, e.g.
$newfile="{$id}.txt";
$file = fopen ($newfile, "w+");
fwrite($file, $output);
fclose ($file);
ob_end_clean();
And use w+
so it will create a new file if one doesn't already exist.
Change the line in your php file:
$newfile="output.txt";
change it to:
$newfile="{$id}.txt";
This will work!!!!
You are giving the file name statically. Just give name of file dynamically like this
$filename = $id.'.txt';