Home > Software design >  Forcing a new line when using php to write to a .txt document
Forcing a new line when using php to write to a .txt document

Time:12-23

I am using an html form, and some php script to write data to a .txt document.

It works fine, but I would like to start a new line with each entry.

I have experimented with some code suggestion, such as '\r\n' and 'nl2br'.

But I believe I am doing it wrong. This is where I left off...

<!DOCTYPE html>
<html>
<head>
</head>
<body >

<form method="POST">

<input type="text" name="textdata" id="textdata">

<input type="submit" name="Submit">

</form>

<script>

<?php
          
if(isset($_POST['textdata']))
    {
    $data=$_POST['\r\n' . 'textdata'];
    $fp = fopen('data.txt', 'a');
fwrite ($fp, $data);
fclose($fp);

    }
    ?>

</script> 

</body>
</html>

CodePudding user response:

You are actually doing wrong. Use post request data as it is.

if(isset($_POST['textdata'])){
    $data= "\n" . $_POST['textdata'];
    $fp = fopen('data.txt', 'a');
    fwrite ($fp, $data);
    fclose($fp);
}
  • Related