Advertisement

Writing to file using PHP Form - Step 3: PHP Codes

Step 3: PHP Codes

Now that we have completed making the form page, we now need PHP coding to write the data to text file.

To do this, again open form.php On the top of everything, write this code:

PHP Code:
if (isset($_POST['submit'])) {
// Do something
}
?>

This means if $_POST['submit'] is set, where ’submit’ is the “name” of the submit button, then do something.
We used here $_POST[] because we used form method=”POST”, if we had used method=”GET “, then we would have used $_GET[] here.
Now we need it to do something if the submit button is set.
So, to do this, we will add some code on our current code.
We will add everything we need, after or by replacing “// Do something“.
Before we make the script write to file, we need to make sure of following things:
If on Linux, file must have writing permission , that is, CHMOD 777
Now start writing code !

PHP Code:
$filename = "name.txt"; // Where the "name" is name of file it will create if file doesnt exist, and will write data on it.

Before writing to file, we need to open it, so we are using fopen():

PHP Code:
$open = fopen($filename, "a");

We used “a” as the mode of fopen, which means open the file for writing purpose, and if file doesnt exist, create it.
Here is the complete list of modes of fopen():

Modes Description
r Read only, places the file pointer at the beginning of the file
r+ Read and Write, places the file pointer at the beginning of the file
w Write only, clears the file content. If file doesnt exist, creates it.
w+ Read and Write only, clears the file content. If file doesnt exist, creates it.
a Appends the file content. Writes to the end of the file. If file doesnt exist, creates it.
a+ Reads and Appends the file content. Opens and writes to the end of the file.If file doesnt exist, creates it.
x Creates the file and opens for Writing only, places the file pointer at the
beginning of the file. If the file already exists, returns FALSE and gives
an error message.
x+ Creates the file and opens for Reading and Writing, places the file pointer
at the beginning of the file. If the file already exists, returns FALSE and
gives an error message.

Note: If the fopen() function is unable to open the specified file, it returns 0 (false).

Now we will assign all the data collected by text fields into variables.
To do this, we will use this code:

PHP Code:
$name = $_POST['name'];
$age = $_POST['age'];
$hidden = $_POST['hidden'];

As you can see, we have assigned Name to $name and Age to $age.
Now we will write the data to text file using fwrite()

What is fwrite() ?

fwrite()is very simple PHP funtion which is use to write data to files.

To write the data to files, write this code :

PHP Code:
fwrite($open, "Name:\t$name\r");
fwrite($open, "Age:\t$age\r");
fwrite($open, "Hidden:\t$hidden\r");

As you can see, we had to use fwrite() two times towrite 2 statements, but we can also use it once for all statements, using foreach() , which I will teach you later.
We also used “\r” here, which gives linebreak after each statement, we can also use “\n” for that though.
However, we can also use fputs() for that, which you will learn in future Tutorials.
We also used “\t” here, to give a Tab space after Name: , Age: and Hidden: in the file.
However, it is not necessary.
Now that our data is written in file, we need to close it, to close, write this code:

PHP Code:
fclose($open);

So that our whole code will look like this :

Code:
<?php
if (isset($_POST['submit'])) {
$filename = “name.txt”; // Where the “name” is name of file it will create if file doesnt exist, and will write data on it.
$open = fopen($filename, “a”);
$name = $_POST['name'];
$age = $_POST['age'];
$hidden = $_POST['hidden'];
fwrite($open, “Name:\t$name\r”);
fwrite($open, “Age:\t$age\r”);
fwrite($open, “Hidden:\t$hidden\r”);
fclose($open);
}
?>
<html>
<head>
<title>This will be your Page’s title</title> <!– You can change the title, set whatever you want –>
<link rel=”stylesheet” type=”text/css” href=”style.css” />
</head>
<body> <!– Printing on page starts from here –>
<!– This is a comment and will not be printed on page –>
<form action=”" method=”POST”>
Name: <input type=”text” name=”name” /> <br />
Age: <input type=”text” name=”age” /> <br />
<input type=”hidden” name=”hidden” value=”Hello, I submitted the form” />
<input type=”submit” name=”submit” value=”Write it !” />
</form> <!– Ending the form –>
</body>
</html>

You will see in name.txt, our hidden form field will be written in the file.
And our form will give out this result:

Result:
Name: Bilal Shakir Ghouri
Age: 15
Hidden: Hello, I submitted the form

And yes, you can remove all the comments if you want to.
Thats all folks !
Happy Learning, and remember, Practice makes Perfect, so keep practicing the PHP, CSS and HTML/ XHTML codes you learnt here.

Related Tutorials



13 responses. Wanna say something?

    Comment #1
  1. man-a-nizer
    Posted on Jun 5, 2008 at 20:21:36

    dude how do i store like a boolean or something in thetext file and check if its true or false???

  2. Comment #2
  3. Bilal Ghouri
    Posted on Jun 8, 2008 at 23:45:50

    Hello man-a-nizer,

    You can do it like this :

    < ?php
    if (isset($_POST['submit'])) {
    $filename = “name.txt”; // Where the “name” is name of file it will create if file doesnt exist, and will write data on it.
    $open = fopen($filename, “a”);
    $boolean = $_POST['are-you-human'];
    if ($boolean == 'yes')
    {
    $write = 'true';
    } else {
    $write = 'false';
    }
    fwrite($open, “Name:\t$write\r”);
    fclose($open);
    }
    ?>

    Note:
    In this case, we are using form field with option fields/radio button/check boxes/ input text fields.
    It does a simple job, if user input is yes, then it will write “true” to text file, else it will write “false”

    I hope this helps you :)

  4. Comment #3
  5. senthil
    Posted on Aug 5, 2008 at 17:50:50

    hi

    tell me how to write code to post user comments on the same page

  6. Comment #4
  7. Bilal Ghouri
    Posted on Aug 9, 2008 at 00:22:35

    What do you mean? Please be more clear..

    Thanks

  8. Comment #5
  9. Girish
    Posted on Aug 14, 2008 at 00:52:34

    I wanted to know abt storing data in Mysql. I dont have knowledge abt programming. Can u write code for me. i want to create one page website. where there will be contact form. I have a free webhosting account.

  10. Comment #6
  11. Joey Jean
    Posted on Aug 21, 2008 at 20:16:16

    Hello, I was wondering if you could help lead me in the right direction for this project. I am trying to build a script that will allow a user to modify or add to a xml file. I would need it to display the content and then make changes when it is submitted.

  12. Comment #7
  13. Curtis
    Posted on Nov 13, 2008 at 04:47:50

    I can’t seem to get your script to work. It will run without error, however, there are no changes to the name.txt file. I’ve changed the permissions on the server, but the file remains at 0 bytes. When I run your code verbatim, I get an parsing code with the “:” in the fwrite lines. When I change the code to this:

    The code runs fine, but nothing is written to the file.

    Can you help? My goal is to write html form data to a file on the server using php. So far, no joy.

  14. Comment #8
  15. Curtis
    Posted on Nov 13, 2008 at 04:49:02

    This is my code….for some reason it didn’t copy into my post..

  16. Comment #9
  17. Curtis
    Posted on Nov 13, 2008 at 04:49:49

    What is up with the comments here…I can’t copy and paste the code into the text box?

  18. Comment #10
  19. Jimmy
    Posted on Dec 9, 2008 at 09:17:21

    Is there anyway to save the file to a different directory? If so, is it possible to create the file name based of say the information put into the “Name” field? Thanks for the script!

  20. Comment #11
  21. Bilal Ghouri
    Posted on Dec 9, 2008 at 09:54:09

    Yes, all you have to do is put the exact path of file in this variable :

    $filename = “path/name.txt”;

  22. Comment #12
  23. Jimmy
    Posted on Dec 10, 2008 at 11:10:18

    Thanks for the quick response. Is it possible to have the file that was created fopen, emailed to a mail address as an attachment?

  24. Comment #13
  25. Bilal Ghouri
    Posted on Dec 10, 2008 at 14:18:40

    Yes it is possible, but it is rather complex to explain here in comments, I will write a new tutorial for it as soon as I get some time.
    You subscribe to the RSS Feed so that you dont miss it.
    I am currently working on a new design for this website.

    Thanks,
    Bilal

Post a Comment