-2

I have made a form, which adds some lines inside another file, called data.php. Here is the code:

<?php
if (isset($_POST['post'])){

    // GET EMAIL
    $post_text = $_POST["post_text"];
    if($post_text == '') $error = "Post content is empty";
    $filename = getcwd() . "data.php"; 
    $line_i_am_looking_for = 1; 
    $lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
    $lines[$line_i_am_looking_for] = $post_text; 
    file_put_contents( $filename , implode( "\n", $lines ) );
    ?>

    <html>
    <head>
    </header
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <?php
            if ($error) {
            ?>

                <script type="text/javascript">
                    // close the errordiv in secs
                    window.setTimeout("close();", 4000);

                    function close(){
                        document.getElementById("errordiv").style.display="none";
                    }
                </script>

            <?php   
            }   
            ?>
            <div id="errordiv" style="display:block; color:#FF3535">
                <b><?php if($error) echo $error; ?></b>
            </div>
            Email:
            <input name="post_text" size="30" type="text">

            <input class="button" value="Submit" type="submit" name="post">
        </form> 
    </body>
</html>

When I open index.php, enter some words in the text box and submit the form, I want to write the entered text in the second line of data.php file. But it's not working.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

1 Answers1

0

I was looking into the code and I found the answer my self. There was a missing } in my code. The edited code is here:

<?php
if (isset($_POST['post'])){

// GET EMAIL
        $post_text = $_POST["post_text"];
if($post_text == '') $error = "Post content is empty";
$filename = getcwd() . "/data.php"; 
$line_i_am_looking_for = 1; 
$lines = file( $filename , FILE_IGNORE_NEW_LINES ); 
$lines[$line_i_am_looking_for] = $post_text; 
file_put_contents( $filename , implode( "\n", $lines ) );
}
?>

<html>
<head>
</header
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
if($error){
?>

<script type="text/javascript">
// close the errordiv in secs
window.setTimeout("close();", 4000);

function close(){
document.getElementById("errordiv").style.display="none";
}
</script>

<?php   
}   
?>
<div id="errordiv" style="display:block; color:#FF3535"><b><?php if($error) echo $error; ?></b></div>
Email:
<input name="post_text" size="30" type="text">

<input class="button" value="Submit" type="submit" name="post">
</form> 


</body>
</html>