前提・実現したいこと
ここに質問の内容を詳しく書いてください。
質問内容
掲示板で、消去ボタンを押すと、投稿が消去できるものを作ったのですが、
消去ボタンを押すと、datのほうでは、すでに該当の箇所が消去されるのですが、画面には反映されず、一度リロードしないと表示されません。
この理由は、どういうものなのでしょうか?
一度のクリックですぐに表示をさせる方法はありますでしょうか?
下のコードは、消去する際に表示される画面の部分になります。
発生している問題・エラーメッセージこ
php
コード
<?php date_default_timezone_set('Asia/Tokyo'); $dataFile = 'bbs.dat'; $passFile = 'pass.dat'; require_once('function.php'); $kaku = ""; $posts = file($dataFile, FILE_IGNORE_NEW_LINES); // var_dump($posts); // $posts = array_reverse($posts); // var_dump($posts); if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['deletepass'])){ $deletepass = trim($_POST['deletepass']); $kaku = "消去しました"; $delete = (int)$deletepass; $fp = fopen($dataFile, 'w'); foreach ($posts as $key => $post){ if ($delete !== ''){ list($usercomment, $username, $postedAt) = explode("\t", $post); if ($key == $delete - 1){ var_dump($key); var_dump($delete); // $post = "消去しました".$postedAt; $usercomment = "消去しました-"; $username = "不明"; $postedAt = $postedAt; } else{ $usercomment = $usercomment; $username = $username; $postedAt = $postedAt; } $newData = $usercomment. "\t". $username. "\t". $postedAt."\n"; fwrite($fp, $newData); } } fclose($fp); } $postedAt = date('Y-m-d H:i:s'); $posts = array_reverse($posts); var_dump($newData); ?> <!DOCTYPE html> <html lang="ja"> <meta charset="utf-8"> <form action="" method="POST"> <input type="text" name="deletepass" placeholder="delete_pass" value="<?php echo h($deletepass, ENT_QUOTES, 'UTF-8'); ?>"> <?php echo $kaku; ?> <input type="submit" value="消去"><br/> </form> <form action="Epass.php" method="POST"> <input type="submit" value="戻る"><br/> </form> <h2>投稿一覧(<?php echo count($posts); ?>件)</h2> <ol reversed> <?php if (count($posts)) : ?> <?php foreach ($posts as $post) : ?> <?php list($usercomment, $username, $postedAt) = explode("\t", $post); ?> <li><?php echo h($usercomment); ?> (<?php echo h($username); ?>) - <?php echo h($postedAt); ?></li> <?php endforeach; ?> <?php else : ?> <li>まだ投稿はありません。</li> <?php endif; ?> </ol> </html>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/27 11:41