我在这个网站上搜索了一个答案,但找不到任何答案.
我有一个表单,我想将输入的内容写入txt文件.为了简单起见,我只写了一个简单的表单和一个脚本,但它一直让我得到一个空白页面.这就是我得到的
<html>
<head>
<title></title>
</head>
<body>
<form>
<form action="myprocessingscript.php" method="post">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
<a href='data.txt'>Text file</a>
</body>
Run Code Online (Sandbox Code Playgroud)
这是我的PHP文件
<?php
$txt = "data.txt";
$fh = fopen($txt, 'w+');
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both fields are set
$txt=$_POST['field1'].' - '.$_POST['field2'];
file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt
exit();
}
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
?>
Run Code Online (Sandbox Code Playgroud)
Flo*_*oby 41
您的表单应如下所示:
<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
</form>
Run Code Online (Sandbox Code Playgroud)
和PHP
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "\r\n";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
Run Code Online (Sandbox Code Playgroud)
我写信给我是/tmp/mydata.txt
因为这样我确切地知道它在哪里.使用data.txt
写入当前工作目录中的该文件,我在您的示例中一无所知.
file_put_contents
为您打开,写入和关闭文件.不要乱用它.
进一步阅读: file_put_contents
你的问题是多余的,因为<form>
你有你的数据在云GET
的方法,你在访问数据PHP
使用POST
.
<body>
<!--<form>-->
<form action="myprocessingscript.php" method="POST">
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
122839 次 |
最近记录: |