我想使用PHP从文本框中获取文本并将其添加到文本文件中

时间:2016-12-14 17:00:26

标签: php html text-files

我编写了一个用HTML运行的PHP脚本,它将文本输入到文本框中并将其放在文本文件中,但是,代码不起作用,我无法弄清楚原因。我对PHP很新,如果有人可以提供帮助,那就太棒了:D

<!DOCTYPE HTML>
<html>
    <head>
        <title>Altering text files</title>
    </head>
<body>

<form name ="form1" method ="post" action = "">

<input type = "text" name = "string">
<input type = "submit" name = "submit" value = "Add text">

</form>

</body>
</html>
<?php

$file = "./lines.txt";
$write = $_POST['string']

file_put_contents($file , $write, FILE_APPEND);

?>

2 个答案:

答案 0 :(得分:1)

<!DOCTYPE HTML>
<html>
    <head>
        <title>Altering text files</title>
    </head>
<body>

<form name ="form1" method ="post" action = "">

<input type = "text" name = "string">
<input type = "submit" name = "submit" value = "Add text">

</form>

</body>
</html>

<?php
if(isset($_POST))
{
    var_dump($_POST);
    echo 'foo!';
    /*$file = "./lines.txt";
    $write = $_POST['string'];

    file_put_contents($file , $write, FILE_APPEND);*/
}

?>

测试它以查看是否打印了某些内容

答案 1 :(得分:0)

尝试在$ _POST ['string']后添加分号 也尝试显示错误,以了解是否有任何错误