Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

write to a text file from HTML form using PHP

I wrote a html form just with an email field. I want to transfer that email that the user inputs into a text file on my web server.

When I click on the submit button I get a white screen. Nothing is written to my text file. I also want to redirect the user to another page when they click the submit button. Is this possible? I am a newbie.

<?php

$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
fclose($file); 
print_r(error_get_last());

?>

<form action= "emaillist.php" method="post" name="email ">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="email" value="submit"><br>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In addition to what Andy G said about changing the name of submit button:

<?php
//Get the email from POST
$email = $_REQUEST['email'];
$file = fopen("emaillist.txt","a+");
fwrite($file,$email);
print_r(error_get_last());

//redirect
header("Location: http://www.example.com/");

Don't leave any blank lines between <?php and the beginning of the file, otherwise redirect won't work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...