<?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?