我正在尝试使用此HTML表单从最终用户收集信息,并使用PHP将其保存到文本文件中。我正在寻找各种方法,我可以通过假设文本文件不存在来完成此任务,并检查和附加文件是否存在。有没有人有任何建议?
Html表格:
<form id="newuser" method="post" action="newUser.php">
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full Name">
<label for="email">E-mail</label>
<input type="email" name="email" placeholder="hello@world.com">
<label for="birthday">Birthday</label>
<input type="date" name="birthday" min="1929-12-31">
<label for="phone">Phone</label>
<input type="tel" name="phone" placeholder="ex. (555) 555-5555">
<label for="message">Question/Comment</label>
<textarea name="message"></textarea>
<label>Check this box if you agree to the website
<a href="terms.php">terms</a>
<input type="checkbox" name="agreetoterms" value="Agree"> </label>
<input type="submit" name="submit" id="submit" value="Join Now" />
</fieldset> </form>
答案 0 :(得分:0)
我真的不知道还有什么要添加,所以让我们希望通过将用户的表单提交记录到文本文件来为您解决问题提供更多指导。除了“检查”之外,你可以按照以下方式进行操作
<?php
/* newuser.php */
if( $_SERVER['REQUEST_METHOD']=='POST' ){
/*
A path to where you wish to store the file, preferably outside of the document root.
The example path below is within the document root because there is no way of
knowing your directory structure.
*/
$saveto=$_SERVER['DOCUMENT_ROOT'] . '/folder/file.txt';
if( !empty( $_POST ) ){
/*
Add the POSTed form contents to your file.
------------------------------------------
> The return of `file_put_contents` is the number of bytes written to the designated file.
> `print_r( $var, true )` makes the output suitable for writing to file.
> `FILE_APPEND` & `FILE_TEXT` are two of the possible options `file_put_contents` accepts and are suitable for your question
*/
$bytes = @file_put_contents( $saveto, print_r( $_POST, true ), FILE_APPEND | FILE_TEXT );
/* Process the form data however you would normally */
}
}
?>