Adding text from a form to a .txt file

Hi. I am trying to learn PHP. I created a form with action=“member.php” method=“POST”
I just want to add the information from the form to a .txt file for now.

I did this and it does not seem to be working. [php]<?php
$name = $_POST[‘name’];
$firstName= $_POST[‘firstName’];
$age = $_POST[‘age’];
$sexe = $_POST[‘sexe’];
$category = $_POST[‘category’];

$tmpE=fopen(“members.txt”,“r+”) or die (“Cannot open file”);
$line="\n".$name."\t".$firstName."\t".$age."\t".$sexe."\t".$category."; \n";
fwrite($tmpE,$ligne);
fclose($tmpE);
?>[/php]

Also I’ll be having another form in my index.html in which I can search for a member within that .txt file.

Thanks in advance for the help :slight_smile:

oops I know I wrote fwrite($tmpE,$ligne);

instead of

fwrite($tmpE,$line);

but it is still not working ): huhu

As far as the simple writing function to a text file goes, you should do something like this …

[php]

<?PHP $name = $_POST['name']; $firstname= $_POST['firstname']; $age = $_POST['age']; $sex = $_POST['sex']; $category = $_POST['category']; $file = fopen("data.txt","w"); echo fwrite($file,"'$name', '$firstname', '$age', '$sex', '$category'"); fclose($file); ?>

[/php]j

You will need to change your variable names to match your own. I’ve tested this and it works.

Thanks deek! Im busy right now I’ll try later but it’s very appreciated :slight_smile:

There’s really nothing wrong with your code. Did you create the members.txt file and give it writable permissions? You could also try using “a” instead of “w” which will attempt to create the file if it does not exist.

Sponsor our Newsletter | Privacy Policy | Terms of Service