-1

I am new to php, but I can't figure out what the problem is. I am trying to make a text box for string entry.

<?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> 
echo $_GET['subject'];
<html> file_put_contents("/file/location", $_GET['subject'], FILE_APPEND); </html>
?>

The first part runs fine, but when I get to the <html> it starts to spit out all sorts of errors.

BlackNetworkBit
  • 768
  • 11
  • 21
Alex O'Connor
  • 80
  • 1
  • 9

2 Answers2

2

You're mixing html with php without properly enclosing. Here's an edited version:

<?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); 
 ?>
dustytrash
  • 1,568
  • 1
  • 10
  • 17
2

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141