Jump to content

Creating a line break after writing to a file??


adam1984

Recommended Posts

Below is the code I have asking a user for their first and last name. After they submit, it saves the input to a separate file. My code works, but my processed file doesn't look pretty. Here is where I am having the trouble. For instance, if the user types in Bruce Willis, and then my next user types in Quentin Tarantino.. my .txt file where the names are written will look like this:

---------------------------

Bruce Willis Quentin Tarantino

---------------------------

 

Ideally, I would like a line break between each name. That would make it easier for me to "count lines" of the .txt file later to identify how many users have inputted names.

 

Here is my code if anyone is available to give me some tips on how I could achieve this. Thank you in advance!!

 

<html>

<head>

</title>

</title>

</head>

<body>

 

<?php

 

$first= $_POST['firstname'];

$last = $_POST['lastname'];

$fullname= "$first $last ";

 

$filename="people_processed.txt";

$fp = fopen($filename, "a" ) or die("Couldn't open file");

fwrite($fp, $fullname);

fclose($fp);

 

} else {

 

$error = "You didn't enter a required field";

}

}

 

?>

 

<?php echo $error; ?>

<form method="post" action="">

<input type="text" name="firstname" id="firstname" />

<input type="text" name="lastname" id="lastname" />

<input type="submit" name="submit" id="submit" />

</form>

 

</body>

</html>

Link to comment
Share on other sites

roopurt18>>

 

I tried that but i'm not seeing the result :'(

Is it because my people_processed file is a .txt?

 

When I open the file I still see each name  just running into eachother...

would it help if people_processed was an .html file?

 

I also tried creating a function and storing it in a variable to try it that way, but i was having trouble with that too.....

 

Link to comment
Share on other sites

Only if you intend to keep using "\n" as your newline character and want to view it on windows.  You just have to be mindful of which OS this text file will be viewed on.  Most text editing programs are smart enough to treat \n, \r, and \r\n the same, meaning they cause a carriage return linefeed.  Notepad is the only text editor I'm aware of that doesn't know what to do with them.

 

So if you're generating text files that will be e-mailed to non-tech savvy users, then you should use \r\n.  Otherwise \n is typically preferred.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.