As I said in comments, you're injecting pure HTML into PHP.
You have to close off the PHP tag, then reopen.
The <html>
tag in this <html> file_put_contents
and related </html>
needs to be removed also.
There should probably be a trailing slash for /file/location
if that gives you a problem, but might not be needed, it's just a side note here.
<?php
$file = fopen("/file/location","r");
while(!feof($file)){
echo fgets($file);
echo "<br>";
}
fclose($file);
?>
<html>
<form name="form" action="" method="get">
<input type="text" name="subject" id="subject" value="Car Loan">
</form>
</html>
<?php
echo $_GET['subject'];
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
However, you need to use a conditional statement here, since you will get an undefined index notice for the echo'd GET array and in the file_put_contents
seeing that your entire code is in the same file; action=""
suggests it. Error reporting will help you here.
I.e.: if(!empty($var)){...}
and/or if(isset($var)){...}
.
Make sure also that the folder can be written to and that proper permissions have been set for it.
Your folder declaration might also require it to be a full server path.
Consult the manual on fopen()
http://php.net/manual/en/function.fopen.php.
I.e.: fopen("/var/usr/public_html/file/location","r");
.
The same might also happen for file_put_contents()
. If what you have now gives you a problem, try a server path instead.
Footnote:
You could place the closing </html>
after
file_put_contents("/file/location", $_GET['subject'], FILE_APPEND);
?>
since echoing something outside the HTML tags will throw something in the console/HTML source.