获取文本框文本(Get textbox text)

编程入门 行业动态 更新时间:2024-10-27 23:21:46
获取文本框文本(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"

更多推荐

本文发布于:2023-08-02 05:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1371000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   文本   text   textbox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!