Hello guys,

I'm trying to save/store data from a form to a text file. The problem is that the information sent from the form does not seem to be storing in the text file as whenever I enter its url the page is just empty. The code is as follows:

The simple form (database.php)

<form action = "ProcessFormData.php" method = "post">

Name <input type="text" name="name">
Age <input type="text" name="age">

<input type="submit" value="Go Go">

The php file that is supposed to transfer the form data onto the text file

<?php
$name = $_POST['name'];
$age = $_POST['age'];
$data = "$name ¦ $age\n";

$fh = fopen("data.txt", 'a') or die ("can't open file");

fwrite($fh, $data);

fclose($fh);

echo 'file has been saved';
?>

The text file is called data.txt. After doing a little googling I have set chmod 777 permissions on FTP however the data.txt file is still empty and whichever information I enter into the form (database.php) does not seem to get saved on data.txt.

What am I doing wrong here? What do I need to do to view the form data?

Member Avatar for LastMitch

@ibn_sumal

There's nothing wrong with your <form> and php file and your data.txt

This is your <form>:

<form action="ProcessFormData.php" method="post">
Name <input type="text" name="name">
Age <input type="text" name="age">
<input type="submit" value="Go Go">

This is you ProcessFormData file:

<?php
$name = $_POST['name'];
$age = $_POST['age'];
$data = "$name ¦ $age\n";
$fh = fopen("data.txt", 'a') or die ("can't open file");
fwrite($fh, $data);
fclose($fh);
echo 'file has been saved';
?>

When run your code on my server it works fine. When I open the data.txt has info in it.

This is the data file:

Mitch | 100

What are you trying to do? The code works fine.

Member Avatar for LastMitch

@ibn_sumal

Perhaps the path is the problem?

Maybe, it must be the path.

When I ran your code I had all 3 files in a folder. So the code works! Try to link the path.

I assume that test is a folder?

So put all 3 files in the test folder. Put <form> html & ProcessFormData.php & data.txt in the test folder then run the code. It should work

It's not the path. I've put all 3 files under public_html but still get an blank page when opening data.txt.

If it works for you i'm guessing that it must be the free hosting service which I am using? However I am able to view form information when using the GET or POST method without attempting to store the data. Quite weird.

Member Avatar for LastMitch

@ibn_sumal

It's not the path. I've put all 3 files under public_html but still get an blank page when opening data.txt.

Well, the public_html or www is the same thing.

If it works for you i'm guessing that it must be the free hosting service which I am using?

I don't think so. Try this:

Put your ProcessFormData.php & data.txt in the test folder and leave the <form> html on public_html or www.

So on your <form> it should be like this:

<form action="../test/ProcessFormData.php" method="post">
Name <input type="text" name="name">
Age <input type="text" name="age">
<input type="submit" value="Go Go">
Member Avatar for LastMitch

@ibn_sumal

I try your link and enter the info but when I open your data file the data didn't appear.

I did what you suggested LastMitch but still doesn't work.

I try your link and enter the info but when I open your data file the data didn't appear.

Yes this is the problem, for some reason the data doesn't appear.

Hi,

Try adding, these codes just below the fclose($fh);

echo substr(sprintf('%o', fileperms('/test/test.txt')), -4);

Make sure to put the proper loction of the test.txt.. Let us know of the 4 intengers printed on your screen.

Member Avatar for LastMitch

@ibn_sumal

Yes this is the problem, for some reason the data doesn't appear.

Try this since you originally put the <form> html & ProcessFormData.php & data.txt file in the public_html or www

On your ProcessFormData.php add this

Instead of this:

$fh = fopen("data.txt", 'a') or die ("can't open file");

change it to this:

$fh = fopen($_SERVER['DOCUMENT_ROOT'] . "/data.txt","a"); or die ("can't open file");

Now your file looks like this:

<?php
$name = $_POST['name'];
$age = $_POST['age'];
$data = "$name ¦ $age\n";
$fh = fopen($_SERVER['DOCUMENT_ROOT'] . "/data.txt","a"); or die ("can't open file");
fwrite($fh, $data);
fclose($fh);
echo 'file has been saved';
?>

@veedeoo,

I entered your code under fclose($fh) as follows:

echo substr(sprintf('%o', fileperms('/test/data.txt')), -4);

Changed the text.txt to data.txt however I left the rest untouched, this is the error I received.

PHP Error Message Warning: fileperms() [function.fileperms]: stat failed for /test/data.txt in /home/a6126152/public_html/test/ProcessFormData.php on line 11
0file has been saved

@LastMitch,

I'm guessing you want me to put all 3 files back under public_html?

Member Avatar for LastMitch

@ibn_sumal

Changed the text.txt to data.txt however I left the rest untouched, this is the error I received.

echo substr(sprintf('%o', fileperms('/test/data.txt')), -4);

The code that veedeoo provided should work.

Did you put the data.txt back in the test folder before you ran the veedeoo code?

I'm guessing you want me to put all 3 files back under public_html?

Put it back since you originally put the <form> html & ProcessFormData.php & data.txt file in the public_html or www.

Then run the code

@LastMitch

After making the change to the $fh I get this error

Parse error: syntax error, unexpected T_LOGICAL_OR in /home/a6126152/public_html/ProcessFormData.php on line 5

..

Did you put the data.txt back in the test folder before you ran the veedeoo code?

Yes I did.

Member Avatar for LastMitch

After making the change to the $fh I get this error

I think I forgot to leave out the ; semi-colon in here:

from this:

$fh = fopen($_SERVER['DOCUMENT_ROOT'] . "/data.txt","a"); or die ("can't open file");

to this:

$fh = fopen($_SERVER['DOCUMENT_ROOT'] . "/data.txt","a") or die ("can't open file");

I now get this error.

PHP Error Message

Warning: fopen() [function.fopen]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/data.txt) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/a6126152/public_html/ProcessFormData.php on line 6

Free Web Hosting

PHP Error Message

Warning: fopen(/usr/local/apache/htdocs/data.txt) [function.fopen]: failed to open stream: Operation not permitted in /home/a6126152/public_html/ProcessFormData.php on line 6

Free Web Hosting
can't open file

Member Avatar for LastMitch

@ibn_sumal

Warning: fopen() [function.fopen]: open_basedir restriction in effect. File(/usr/local/apache/htdocs/data.txt) is not within the allowed path(s): (/home/:/usr/lib/php:/tmp) in /home/a6126152/public_html/ProcessFormData.php on line 6

The error means that there is an open_basedir restrictions on your host server.

To correct it you have need to edit php.ini and set chmod 777 on your test folder.

To correct it you have need to edit php.ini and set chmod 777 on your test folder.

I recommend using 755 (777 is too vulnerable). I think the rights were the problem in the first place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.