将条目表单详细信息写入文本文件

时间:2014-05-28 08:50:04

标签: php html forms file

我正在尝试使用PHP代码将提交表单中的数据输入到文本文件中。我正在努力创建php文件以接收下面的数据并将其保存在文本文件中,最好在新标签中打开它。

<html>
<head>
<title>Entry Form</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Login Code:<input type="text" size="12" maxlength="36" name="Lcode"><br />
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
Gender: <br />
Male:<input type="radio" value="Male" name="gender"><br />
Female:<input type="radio" value="Female" name="gender"

2 个答案:

答案 0 :(得分:0)

提交表单后,您可以获取表单数据并将其写入file_put_contents()

这里解释了

功能 http://www.php.net/manual/en/function.file-put-contents.php

您提交的是与表单相同的页面,因此我建议您检查post数组,然后获取数据并将其写入文件:

$file = 'content.txt';
$content.= $_POST["Fname"]."\n";
$content.= $_POST["Lname"]."\n";

等...

 // Write the content to the file
    file_put_contents($file, $content);

答案 1 :(得分:0)

试试这个

HTML

<html>
<head>
<title>Entry Form</title>
</head>
<body>
<form method="post" action="">
Login Code:<input type="text" size="12" maxlength="36" name="Lcode"><br />
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
Gender: <br />
Male:<input type="radio" value="Male" name="gender"><br />
Female:<input type="radio" value="Female" name="gender"></br>
<input type="submit" value="Submit" name="submit">
</form>

PHP

<?php
if(isset($_POST['submit'])){
    $fp = fopen('myfile.txt', 'a+');//This will create a file named myfile.txt in your application base path
    unset($_POST['submit']);
    foreach ($_POST as $key => $value) {
        fwrite($fp,$key.' = '.$value."\n");
    }
    fclose($fp);
}
?>