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

时间:2014-11-09 19:06:13

标签: php html forms post

我正在尝试创建一个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);

?>

对于误导性的标题感到抱歉,我要发布的另一个问题仍然在这里,我忘了编辑标题

3 个答案:

答案 0 :(得分:2)

问题首先是:“没有要处理的帖子数据”

在表单中使用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);

因此,如何格式化字符串(使用逗号或其他方式)并不重要,但它必须是StringCheck the PHP manual

答案 1 :(得分:0)

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

<?php

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

答案 2 :(得分:0)

显然你不知道你在做什么;甚至你的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);

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