Writing html form data to .txt file.

For the last two days I have been trying to get form data from my website written to a .txt file through php. The text file is one line of data, and I want to overwrite this data every time I write to the file. I have tried to incorporate my php with html, and also tried linking the html to the php file. I am new to php, and form data transfer.

OS = Raspberry pi Model B
Server = Apache 2 with php5 installed

Code Below (any help would be great)

(file name myform1.php) Stored in root server directory

[php]

<?php { $varName = $_POST['formName']; $fp = fopen("thermostat.txt", 'r+') or die("cant open file");; fwrite($fp,$varName); fclose($fp); exit; } ?>

[/php]

(file name index.html) Stored in root server directory

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" /> 
	<title>Full Control | Home Automation Made Easy</title>
	<link rel="stylesheet" type="text/css" href="myhome.css"/>
</head>
<body>
	</div>
<h1>Full Control Home Automation</h1>
	</div>	
<h2><em>Thermostat Control</em></h2>
	<div>
		<div id="settemp">
	<h3>Desired Temperature</h3>

	<form action="myform1.php" method="POST">
		<p>
			Set Desired Temperature<br>
			<input type="text" name="formName" maxlength="10" />
		</p>				
		<input type="submit" name="formSubmit" value="Submit" />
	</form>

</body>
</html>

Thanks for any help in advance!

You have errors in your html like closing a div before you have opened it BUT those should not impact what you are trying to do BUT they should be fixed.

Echo out the contents of $varName immediately after its declaration statement to see if the value is available as intended. It will narrow down whether the issue is with writing the file or creating the variable.

I know the html is pretty rough, I threw it together to test this php outside of my webpage. I put in the following, and nothing happens. It is ok to run this as one php file correct, from what ive read php can interact within html.
[php]
$varName = $_POST[‘formName’];
echo $varName;
[/php]

Again I am new to the php web stuff. I write mostly in python.

Thanks for the quick responce!

Try this
[php]

<?php { $varName = $_POST['formName']; $fp = fopen("thermostat.txt", 'r+') or die("cant open file");; fwrite($fp,$varName); fclose($fp); } ?>

[/php]

and this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
   <title>Full Control | Home Automation Made Easy</title>
   <link rel="stylesheet" type="text/css" href="myhome.css"/>
</head>
<body>
   <div>
<h1>Full Control Home Automation</h1>
   </div>  
<div>   
<h2><em>Thermostat Control</em></h2>
   <div>
      <div id="settemp">
   <h3>Desired Temperature</h3>

   <form action="myform1.php" method="POST">
      <p>
         Set Desired Temperature<br>
         <input type="text" name="formName" maxlength="10" />
      </p>            
      <input type="submit" name="formSubmit" value="Submit" />
   </form>
</div>
</body>
</html>

Make sure you have an empty thermostat.txt file already on your server.

This isn’t the way I would do it but this works on my test set-up. I would do this all as one php file.

Sponsor our Newsletter | Privacy Policy | Terms of Service