Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write to a text file from HTML form using PHP

Tags:

html

text

forms

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>
like image 889
user2620377 Avatar asked Nov 18 '25 18:11

user2620377


2 Answers

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.

like image 128
user4035 Avatar answered Nov 21 '25 06:11

user4035


In php theres an array called $_POST that stores all of the variables from a form when you post it. So $email would be found in $_POST['email']

like image 34
azrosen92 Avatar answered Nov 21 '25 07:11

azrosen92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!