Jump to content

Form input to text file


OTTO

Recommended Posts

Hey I am having trouble getting this code to work. The problem I think lies within the permissions of my txt file but I could be wrong. I am trying to write what ever data is input into the form into the text file and down below in the php. Thanks for the help!

<body><form id="form1" name="form1" method="post" action=""><label>Please type in a message<input type="text" name="msg" id="msg" /></label><label>and your name<input type="text" name="name" id="name" /></label><p><label>Submit<input type="submit" name="submit" id="submit" value="Submit" /></label></p></form><?php	$msg = $_POST["msg"];	$name = $_POST["name"];	$posts = file_get_contents("posts.txt");	$posts = "$msg - $name\n" . $posts;	file_put_contents("posts.txt", $posts);	echo $posts;?></body>

Link to comment
Share on other sites

How do you mean it's not working? Try checking what the return value of file_get_contents() and file_put_contents() are:

if (!file_get_contents("posts.txt")){	echo "error with file_get_contents()";} if(!file_put_contents("posts.txt", $posts)){	echo "error with file_put_contents()";}

You may need to re-read the file with file_get_contents(); to get the updated version of the file. Try this:

<?php	    $msg = $_POST["msg"];	    $name = $_POST["name"];	    $posts = file_get_contents("posts.txt");	    $posts = "$msg - $name\n" . $posts;	    file_put_contents("posts.txt", $posts);	            $posts = file_get_contents("posts.txt");        echo $posts;?>

Regard, Lab.

Edited by Labtec
Link to comment
Share on other sites

Like I said I think the issue lies with the permissions of the test file. From what I have read I need to change the permission to 666. However I am using windows and am just making the website locally and am not currently hosting it so I don't have an FTP. How do I give it the 666 permissions?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...