Reputation: 1
i am completely new to PHP and as my degree for high school comes closer i decided to make an HTML form for our personal description in our yearbook. But as my PHP file handles the forms POST, i cannot figure out how to seperate the different input and it just outputs everything in one line. My PHP Code is this
<?php
$filename = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6) . '.txt';
if ( !file_exists($filename) ) {
//File put contents stuff here
if(isset($_POST['fname']))
{
$data=$_POST['fname'];
$fp = fopen($filename, 'a');
fwrite($fp, $data);
}
if(isset($_POST['lname']))
{
$data=$_POST['lname'];
$fp = fopen($filename, 'a');
fwrite($fp, $data);
}
}
?>
And my HTML code is:
<!DOCTYPE html>
<html lang="de-DE">
<head>
<title>Erstelle deinen Steckbrief</title>
<meta charset="UTF-8">
<meta name="viewport" content="size=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=7">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="all">
<header>
<div id="ueberschrift">
<h1>Erstelle deinen Steckbrief für die Abizeitung</h1>
<p>Dies ist das Formular für das erstellen deiner Seite für die Abizeitung</p>
</div>
</header>
<div class="form">
<form name="" method="post" action="upload.php">
<label for="fname">Vorname:</label><br>
<input type="text" name="fname" id="fname"><br>
<br>
<label for="lname">Nachname:</label><br>
<input type="text" name="lname" id="lname"><br>
<br>
<label for="dateofbirth">Geburtsdatum:</label><br>
<input type="text" name="dateofbirth" id="dateofbirth"><br>
<br>
<label for="city">Wohnort:</label><br>
<input type="text" name="city" id="city" size="50"><br>
<br>
<label for="groupname">Gruppenname:</label><br>
<input type="text" name="groupname" id="groupname" size="50"><br>
<br>
<label for="futurejob">Späterer Berufswunsch:</label><br>
<input type="text" name="futurejob" id="futurejob" size="50"><br>
<br>
<label for="bteacher">Lieblingslehrer/in:</label><br>
<input type="text" name="bteacher" id="bteacher" size="50"><br>
<br>
<label for="whatialwayswantedtosay">Was ich immer mal loswerden wollte:</label><br>
<input type="text" name="whatialwayswantedtosay" id="whatialwayswantedtosay" size="50"><br>
<br>
<label for="mymotto">Mein Lebensmotto:</label><br>
<input type="text" name="mymotto" id="mymotto" size="50"><br>
<br>
<label for="bestlesson">Lieblingsfach:</label><br>
<input type="text" name="bestlesson" id="bestlesson" size="50"><br>
<br>
<label for="worstlesson">Hassfach:</label><br>
<input type="text" name="worstlesson" id="worstlesson" size="50"><br>
<br>
<label for="noschoolwithout">Ohne das hätte ich meine Schulzeit nicht geschafft:</label><br>
<input type="text" name="noschoolwithout" id="noschoolwithout" size="50"><br>
<br>
<label for="inthreewords">Meine Schulzeit in drei Worten:</label><br>
<input type="text" name="inthreewords" id="inthreewords" size="50"><br>
<br>
<label for="myadvice">Mein Rat an jüngere Schüler:</label><br>
<textarea name="myadvice" id="myadvice" cols="52" rows="5"></textarea>
<br>
<label for="ratingofconstruction">Anzahl Sterne des Baulärms 1-5:</label><br>
<input type="number" name="ratingofconstruction" min="1" max="5" id="ratingofconstruction" size="1"><br>
<br>
Bild zum Hochladen wählen:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Abschicken" name="submit">
<label for=""></label>
<input type="submit" value="Abschicken" name="submit">
</form>
</div>
</div>
</body>
</html>
I will remove the Upload Picture code out of HTML because it doesnt seem to work either. It isnt even in the PHP file anymore.
Upvotes: 0
Views: 740
Reputation: 4889
As others have stated, adding a \n
newline character is what you're missing. Professor Abronsius made your life easier by turning your one-by-one writes into a loop. Don't do it one by one and repeat code, assuming you intend to save all the $_POST
fields submitted!
There are also two other easy options to consider. First, saving as JSON:
$data = json_encode($_POST, JSON_PRETTY_PRINT);
file_put_contents($filename, $data);
This would give you a file with the following contents:
{
"rname": "Arr Name",
"fname": "Foo Name"
}
...and you could easily re-use the data later with the help of json_decode()
.
If you really don't need the array keys, this is by far the simplest way to do it:
$data = implode("\n", $_POST);
file_put_contents($filename, $data);
No need to loop it, or to write each field to the file in a separate call. (In any case, you need to call fopen
just once. Here we just use file_put_contents
instead.) This method would give you a file with the following contents:
Arr Name
Foo Name
If you don't want to include all $_POST
fields, you can filter your post data with:
$allowed = ['rname', 'fname'];
$fields = array_intersect_keys($_POST, array_fill_keys($allowed, ''));
Then, you'd use $fields
as you'd use $_POST
, implementing which-ever of these "bulk saving" methods best fits your use case.
Caution: On using bulk methods for user input commits. A malicious user might tweak the form/submitted data and add fields, and therefore white-listing fields -- specifying as above; or possibly by pulling them from your form definition, since they're already spelled out somewhere -- is recommended, if you intend to re-use the submitted data. You may also want to validate/sanitize the actual contents, but that's a topic for another day.
P.S. While we typically add \n
as the newline character, this is the Linux & MacOS standard. Windows uses \r\n
, and if you're doing this for a Windows context, you might want to use the native line endings instead. Any decent code editor, and also Notepad since 2018 (ref), would work with either, but older software might act up and just fuse lines together.
P.P.S. A note on the use of PHP_EOL
to produce a line-ending. Per the manual, PHP_EOL (string): The correct 'End Of Line' symbol for this platform.
Ie. \n
or \r\n
depending on your OS. Suppose you develop on Windows but later deploy to a Linux server, you might be better off just choosing one (\n
) and sticking with it, to avoid data with mixed newline standards.
Upvotes: 1
Reputation: 33813
You can use \n
, PHP_EOL
or as already stated, chr(10)
to give a new line to your text but IMO using \n
is perhaps simplest when used with something like this:
<?php
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$filename = substr( str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ), 0, 6) . '.txt';
if ( !file_exists( $filename ) ) {
foreach( $_POST as $field => $value ){
/*
`sprintf` can be used to create a formatted string where
variables are substituted for certain placeholders.
the `FILE_APPEND` flag ensures the file is written to
several times with previous data retained.
*/
file_put_contents( $filename, sprintf( "%s: %s\n", $field, $value ), FILE_APPEND );
}
}
}
?>
Disclaimer:
Using \n
within PHP string always make sure it's wrapped with double quotes like:
echo "Some text with new line at the end \n";
If you'll use it with single quote
echo 'Some text with new line at the end \n';
It will literally output with \n
string at the end.
Upvotes: 3
Reputation: 190
In PHP you can use PHP_EOL
to add a line break.
<?php
$filename = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 6) . '.txt';
if ( !file_exists($filename) ) {
//File put contents stuff here
if(isset($_POST['fname']))
{
$data=$_POST['fname'];
$fp = fopen($filename, 'a');
fwrite($fp, $data);
}
if(isset($_POST['lname']))
{
$data=$_POST['lname'];
$fp = fopen($filename, 'a');
fwrite($fp, PHP_EOL . $data);
}
}
?>
Upvotes: 1
Reputation: 55798
you can use for an instance chr(10)
$data=$_POST['fname'] . chr(10);
Upvotes: 3