How can PHP be used to save and display user input on a website?
To save and display user input on a website using PHP, you can use HTML forms to collect user input, then use PHP to process and store the input in a database or file. To display the saved user input, retrieve the data from the database or file and echo it back onto the webpage.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$user_input = $_POST["user_input"];
// Save user input to a file
$file = fopen("user_input.txt", "a");
fwrite($file, $user_input . "\n");
fclose($file);
}
// Display saved user input
$file = fopen("user_input.txt", "r");
while (!feof($file)) {
$line = fgets($file);
echo $line . "<br>";
}
fclose($file);
?>
Keywords
Related Questions
- How can users troubleshoot the issue of only seeing the source code instead of the expected output when opening a PHP test file?
- How can CronJobs be effectively utilized in PHP to automate the process of sending birthday emails to users?
- How can auto-wiring and injecting dependencies through factories be implemented in PHP DIC usage?