将HTML表单输出到txt文件的PHP脚本


PHP script to output an HTML form to txt file?

我正在尝试创建一个PHP文件,该文件将把HTML表单中的文本写入txt文件,然后将用户重定向到一个页面,告诉他们已经完成。

这是我的HTMl表格:

<form action="feedbackScript.php">
 <p>
   Email(for response, optional) 
    <input type="email" name="email" /> <br>
   General feedback and comments
    <textarea name="feedback" cols="100" rows="5"></textarea> <br>
   Rating <br>
    <select name="rating">
     <option value="1">1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     <option value="4">4</option>
     <option value="5">5</option>
    </select>
   Suggestions
    <textarea name="suggestions" cols="100" rows="5"></textarea> <br>
   Bug Report
    <textarea name="bugReport" cols="100" rows="5">(Fill out this form)
     What happened: 
     What you expected to happen: 
     Anything extra: </textarea> <br> <br>
     <input type="submit" value="Submit" name="submit"/>
 </p>
</form>

如何做到这一点?

编辑:最初打算使用这个:

<?php
// Open the text file
$f = fopen("feedbacks.txt", "w");
// Write text
fwrite($f, $_POST['email'] && $_POST['feedback'] && $_POST['suggestions'] && $_POST['bugReport']); 
// Close the text file
fclose($f);
header('Location: suggestFinished.html'.$newURL);
?>

很抱歉误导了标题,我要发布的另一个问题仍然在这里,我忘记编辑标题

最初的问题是:"没有要处理的后期数据"

在表单中使用method='POST',如:

<form action="feedbackScript.php" method="POST">

默认情况下,如果您不使用任何请求方法,它将使用GET请求。

edit之后更新

你有这个:

fwrite($f, $_POST['email'] && $_POST['feedback'] && $_POST['suggestions'] && $_POST['bugReport']);

这不起作用,因为你需要传递一个字符串,例如,像这样的东西:

$string = implode(',', $_POST) . "'n"; // me@ymaiol.com,some feedback text,...,...
fwrite($f, $string);

因此,如何格式化字符串(使用逗号或其他方式)并不重要,但它必须是String。查看PHP手册。

如果使用get方法(默认方法)发送表单,则可以通过$_get全局数组访问用户数据。只需打开一个文件,使用json_encode方法将用户数据的编码字符串插入到文件中(稍后可以使用json_decode方法对其进行解码)

<?php
    $file = fopen('/tmp/user_request.txt');
    fwrite($file, json_encode($_GET));
?>

显然你不知道自己在干什么;甚至你的HTML代码和表单都很糟糕。话虽如此,我会尽力帮忙的。

<?php
/**
 * feedbackScript.php
 * This file writes to .txt file the contents of an HTML form.
 */
// All the values of the HTML form are securely stored in the array $v:
$v = array_map('trim', filter_input_array(INPUT_POST));
// Format your text however you want before it's written to .txt file:
$text = '-- START ' . date('c') . ' --'n'
    . "User email:{$v['email']}'n" 
    . "Feedback and Comments:'n"
    . "{$v['feedback']}'n'n"
    . "Rating: {$v['rating']}'n"
    . "Suggestions: {$v['suggestions']}'n"
    . "Bug Report: {$v['bugReport']}";
// Following lines of code open, write, and close your connection
// to a text file:
$handle = 'path/to/your/txtfile.txt';
$fp = fopen($handle, 'w');
fwrite($handle, $text);
fclose($fp);

附言:在你进入PHP之前修复你的HTML。为什么这个问题发布在jquery下?此外,请确保您的方法是表单中的POST,即method="POST"