I am trying to erase the data stored in $_POST['message']
or set it to null. I'm just trying to clear the data somehow.
Here is my mini chat program.
<?php
require_once("connection.php");
if(isset($_SESSION['log']))
{
if (isset($_POST['message']))
{
sendMessage($_POST['message'], $pdo, $logedInUsername);
}
$query = "SELECT * FROM `chat`";
$search_result = filterTable($query, $pdo);
}
// function to connect and execute the query
function filterTable($query, $pdo)
{
$stmt = $pdo->prepare($query);
$stmt->execute();
$filter_Result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $filter_Result;
}
function sendMessage($message, $pdo, $logedInUsername)
{
if(strlen($message) > 0)
{
$query = "INSERT INTO `chat`(`username`, `message`) VALUES ('$logedInUsername','$message')";
$_POST['message'] = null;
//$_POST['message'] = "";
$stmt = $pdo->prepare($query);
$stmt->execute();
}
}
?>
<!-- html -->
<form method="post">
<input type="text" name="message" id="txt_1" onkeyup='saveValue(this);'/>
<input type="submit" class="send" value="Send">
The problem is when I send a message, then refresh the page the data in $_POST['message']
remains so the sendMessage function runs on page load, which sends the previous message again, which is undesirable.
$_POST['message'] = null;
seems to do nothing when using isset and so does $_POST['message'] = "";
when using strlen to check if greater than 0.