2
0
Files
2025-11-18 13:35:07 +01:00

66 lines
1.3 KiB
PHP
Executable File

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form>
<div>
<label>First Name:
<input name = "fn">
</label>
</div>
<div>
<label for = "ln">Last Name:</label>
<input id = "ln" name = "ln">
</div>
<button>Ab damit</button>
</form>
<?php
//file: file_write.php
$file = "testfile_1.txt";
$fp = fopen($file, 'w'); // open file for writing
if(!$fp) { // is fp = false?
die("Cannot open file!"); // "die" breaks the script
}
// insert one row
/*
$row = "This is a row\n";
if(!fputs($fp, $row)) echo "Error on writing";
else echo "writing succesful";
*/
// insert from an assoziative array
/*
$myArray = [
"firstname" => "Michael",
"lastname" => "Staudt",
"email" => "staudt@wvss-mannheim.de"
];
foreach($myArray as $attr => $value) {
$row = "$attr: $value\n";
fputs($fp, $row);
}
*/
// insert from form
if(!empty($_REQUEST)) { //submit button clicked
$row = "First Name: $_REQUEST[fn] | Last Name: $_REQUEST[ln]\n";
fputs($fp, $row);
}
fclose($fp);
?>
</body>
</html>