0

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.

nedsmith
  • 65
  • 1
  • 10
  • [A cargo cult prepared statement](https://phpdelusions.net/pdo/cargo_cult_prepared_statement) – Dharman Jun 02 '19 at 11:25
  • 1
    Use unset($_POST['message']) – nacho Jun 02 '19 at 11:27
  • The problem is that you send the data and execute the script again each time. Redirect to GET after POST – Dharman Jun 02 '19 at 11:28
  • @nacho - That won't do anything. The issue is that the browser still has the last posted data cached so when you update the page again (in a POST-request), it will do the same request again, sending the same data again. – M. Eriksson Jun 02 '19 at 11:28
  • 1
    Possible duplicate of [How to prevent form resubmission when page is refreshed (F5 / CTRL+R)](https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr) or [Preventing form resubmission](https://stackoverflow.com/questions/3923904/preventing-form-resubmission) – M. Eriksson Jun 02 '19 at 11:29
  • @Dharman I don't understand what you mean by "after POST" do you mean change the ```
    ``` to ```
    ``` then do something with that on the new page load?
    – nedsmith Jun 02 '19 at 12:57
  • Do you want to clear the message after that is sent? – Jijin P Jun 02 '19 at 13:47
  • @JijinP the order of operation is currently: type message, click send, this causes page refresh, on page load sendmessage func is reached with the message in POST['message'], message is printed and then POST['message'] should be cleared. The problem it is not being cleared, so when the user refresh the page, the same message is sent once again. – nedsmith Jun 02 '19 at 14:19
  • Try adding header('Location: /chat_page'); after the execute statement. – Jijin P Jun 02 '19 at 15:08
  • @JijinP That has not worked, but I'm going to try an approach around the lines of not reloading the page and instead just trying to update the table. PS. Also not sure why I didn't see your comment until now. – nedsmith Jun 02 '19 at 16:08
  • I'm going to abandon this for the moment, as it is taking too long. this may help though. https://stackoverflow.com/questions/48736589/reload-php-generated-table-without-page-refresh – nedsmith Jun 02 '19 at 16:51

0 Answers0