douyin9987 2014-10-17 06:57
浏览 16

页面刷新提交我的表单

<?php
$rs = mysql_query("SELECT * FROM users WHERE id='$_SESSION[user_id]'");
while ($row= mysql_fetch_array($rs))
{
     $starter= $row['id'];
     $user_name= $row['user_name'];
}


$starterID=$starter;
$companyID=$_GET['id'];


$input = $_POST['message'];


date_default_timezone_set('Europe/Helsinki');
$timestamp = date('h:i', time());


$file = $companyID." and ".$starterID.".txt";


if (file_exists($file)) {
    $file = $companyID." and ".$starterID.".txt";
} else {
    $file = $starterID." and ".$companyID.".txt";
}


$current = file_get_contents($file);

$current.= "<b>$user_name</b> <br> $input $timestamp
<br>";

if(isset($_POST['message'])){
file_put_contents($file, $current);
}

echo $current;
?>

<form action="" method="post">
<input type="text" name="message" autocomplete="off">
<input type="submit">
</form>

So this is my very simple chat between 2 registered users.

When user with ID 154 starts conversation with ID 156, file 154 and 156.txt is created. This file is where all the messages are stored.

My problem is following: when, let's say user 154 writes a message and sends it by pressing submit button the page refreshes and the message is stored to the file and also printed. After this the input field clears, but if the users refresh the page the same message is being stored and printed again.

What should I do to prevent page refresh submitting the form again?

  • 写回答

2条回答 默认 最新

  • dongqie2355 2014-10-17 07:02
    关注

    Your page still hold the $_POST value. So, if you refresh it. iIts going to print the file again.

    So, add header to redirect your user like this:

    if(isset($_POST['message'])){
        file_put_contents($file, $current);
        header('Location:Your_File_name.php')
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题