获取文本框文本

时间:2012-06-22 16:33:18

标签: php html

单击按钮时,文本框中的文本应写入.txt文件,然后应下载该文件。不知怎的,isset函数不起作用,我已经尝试将php文件与<form&gt;链接起来,但后来我无法阅读文本框文本。

这是我的代码:

<?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>

4 个答案:

答案 0 :(得分:3)

首先,您需要有<form>代码才能提交数据:

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

...

</form>
</body>

您可能还需要将<button type="submit"变为<input type="submit"

答案 1 :(得分:1)

如果php文档在同一个文件中,请添加一个表单标记并回发到同一页面。

<?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>
  <form method="post">
    <textarea name="text" rows="20" cols="100"></textarea><p>
  <input type="submit" value="submit">Download Text</button>
</form>
</body>
</html>

答案 2 :(得分:0)

您尚未在HTML中添加表单TaG。

答案 3 :(得分:0)

在您当前的HTML中,您需要<form>标记。

尝试阅读此here.

相关问题