I'm working on a PHP chat but unfortunately I came across a problem. It's about sending a message via "?save=msg
".
"chat.php?save=msg"
The msg are be written in a database.
My problem is, I do not want to be able to resend the message by reloading the page.
I tried a "Force-Reload"
for the site, but the "?save=msg"
do not vanish through this.
"onclick='chat.php'"
or something like this
PHP-Script
if(isset($_GET['save'])) {
$save = $_GET['save'];
if($save == 'msg') {
//< <> > & &
$uauswahl = trim($_POST['nachrichteneing']);
if ($uauswahl != NULL){
$uauswahl = str_replace("&", "&", $uauswahl);
$uauswahl = str_replace("<", "<", $uauswahl);
$uauswahl = str_replace(">", ">", $uauswahl);
include("str_replace.php");
$uname = $user['benutzer'];
$ava = $user['avatar'];
$statement = $pdo->prepare("INSERT INTO chathistory (msg, name, avatar) VALUES(:msg, :name, :avatar)");
$result = $statement->execute(array('msg'=> $uauswahl, 'name'=> $uname, 'avatar'=> $ava));
if ($result == true){
$success_msg = "Nachricht wurde gesendet.";
} else {
$error_msg = "Beim Absenden der Nachricht scheint ein Fehler aufgetreten zu sein.";
}
} else{
$error_msg = "Nachricht wurde nicht gesendet. </br> Grund: Nachricht darf nicht leer sein.";
}
}
My Form
<form enctype="multipart/form-data" action="?save=msg" method="POST">
<input type="text" id="msgsf" autocomplete="off" name="nachrichteneing" placeholder="Tippe Nachricht...">
<input type="submit" id="msgfb" value="" />
</form>
I hope someone got a idea for this
If you are happy for the page to refresh once to submit the form then you will need to tweak your code to reference $_POST
instead of $_GET
. GET parameters are passed between page reloads by appending the URL where as POST are sent via headers.
From your HTML side you will need to change your action to reflect a page name and then from your code, simply swapping $_GET
for $_POST
should work.