获取文本框文本(Get textbox text)
当我单击按钮时,文本框中的文本应写入.txt文件,然后应该下载。 不知怎的, isset函数不起作用,我已经尝试将php文件与<form >链接,但后来我无法读取文本框文本。
这是我的代码:
<?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>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>最满意答案
首先,您需要有一个<form>标记才能提交您的数据:
<body> <form action="add your php filename here" method="post"> ... </form> </body>您可能还需要将<button type="submit"设为<input type="submit"
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"
更多推荐
发布评论