xoxox xoxox - 1 month ago 6
HTML Question

Get textbox text

When I click the button the text from the textbox should be written to a

.txt
file, which then should be downloaded. Somehow the
isset
function doesn't work, I already tried to link the php file with a
<form
>, but then I can't read the textbox text.

Here's my code:

<?PHP

if(isset($_POST['submit']))
{
$text = $_POST['text'];
print ($text);

$filename = 'test.txt';
$string = $text;

$fp = fopen($filename, "w");
fwrite($fp, $string);
fclose($fp);

header('Content-disposition: attachment; filename=test.txt');
header('Content-type: application/txt');
readfile('test.txt');
}

?>

<html>
<head>
<title>Text Editor</title>
</head>
<body>
<textarea name="text" rows="20" cols="100"></textarea>
<p>
<button type="submit" value="submit">Download Text</button>
</body>
</html>

Answer

Firstly, you need to have a <form> tag to be able to submit your data:

<body>
<form action="add your php filename here" method="post">

...

</form>
</body>

You might also need to make your <button type="submit" into <input type="submit"