Kyel Cassar
Kyel Cassar

Reputation: 1

How can I write the data entered in a form to a text file on button click?

I'll preface this with I'm not a coder nor aspiring to become one.

I just want to play around with something simple.

Please don't feel bad about spoon-feeding me here haha.

All I want is when I hit a my submit button the text entered in the text field is saved to a file called log.text I want it to overwrite each time. Once data has been written I want it to redirect to another page.

Tried this but it doesn't create the file nor write to it even if I create it manually. The redirect also doesn't work because I'm an idiot.

Any help guys? :(

<?php
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
?>

<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>

Upvotes: 0

Views: 1079

Answers (3)

Vigneswaran S
Vigneswaran S

Reputation: 2094

Because you dint checked whether the form is submitted or not. if submited create log. code given below

<?php
if(isset($_REQUEST['submit']))// if to check whether submit name is passed or not  
{
$email = $_REQUEST['email'];
$file = fopen("log.txt","a+");
fwrite($file,$email);
print_r(error_get_last());
header("Location: http://www.example.com/");
}
?>
<html>
<form action= "" method="post" name="form">
<input type="text" name="email">
<br>
<br>
<input type="submit" name="submit" value="submit"><br>
</form>
</html>

Upvotes: 0

Kyel Cassar
Kyel Cassar

Reputation: 1

This guide offered me the solution I was after. Thanks anyway guys! http://www.howtoplaza.com/save-web-form-data-text-file

Upvotes: 0

Bug Hunter 219
Bug Hunter 219

Reputation: 344

It is because the action element of the form is empty. It should be \n

 <form action="action.php(or any other php file that is handling the  form)" method="post" name="form"> 

Upvotes: 2

Related Questions