以下是一个简单的PHP许愿墙代码示例:
index.php:
<!DOCTYPE html>
<html>
<head>
    <title>许愿墙</title>
    <style>
        .wish {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <h1>许愿墙</h1>
    <form action="submit.php" method="POST">
        <textarea name="wish" placeholder="许下你的心愿"></textarea>
        <br>
        <input type="submit" value="许愿">
    </form>
    <h2>所有心愿</h2>
    <?php
    // 从文本文件中读取心愿列表
    $wishes = file("wishes.txt");
    // 遍历心愿列表并显示
    foreach ($wishes as $wish) {
        echo "<div class='wish'>" . htmlspecialchars($wish) . "</div>";
    }
    ?>
</body>
</html>
submit.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 获取提交的心愿内容
    $wish = $_POST["wish"];
    // 将心愿写入文本文件
    $file = fopen("wishes.txt", "a");
    fwrite($file, $wish . "\n");
    fclose($file);
    // 重定向回许愿墙页面
    header("Location: index.php");
    exit();
}
?>
上述代码中,index.php页面包含一个表单,用户可以在文本框中输入心愿并提交。表单的提交动作由submit.php处理。submit.php将接收到的心愿写入一个文本文件中,并通过重定向将用户返回到index.php页面。index.php页面会读取文本文件中的心愿列表,并将其显示在页面上。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站