PHP escribe archivo desde entrada a txt
He buscado en este sitio una respuesta, pero no pude encontrar ninguna.
Tengo un formulario y me gustaría obtener el contenido de la entrada escrito en un archivo txt. Para hacerlo simple, solo escribí un formulario simple y un guión, pero me sigue obteniendo una página en blanco. Esto es lo que tengo
<html>
<head>
<title></title>
</head>
<body>
<form>
<form action="myprocessingscript.php" method="post">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
<a href='data.txt'>Text file</a>
</body>
y aquí está mi archivo PHP
<?php
$txt = "data.txt";
$fh = fopen($txt, 'w+');
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$txt=$_POST['field1'].' - '.$_POST['field2'];
file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt
exit();
}
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
?>