dongmu5815 2019-05-16 07:02
浏览 20

如何在PHP中提交没有“?save = msg”的chatmsg

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') {
            //< &lt;> &gt; & &amp;
            $uauswahl = trim($_POST['nachrichteneing']);
            if ($uauswahl != NULL){
                $uauswahl = str_replace("&", "&amp;", $uauswahl);
                $uauswahl = str_replace("<", "&lt;", $uauswahl);
                $uauswahl = str_replace(">", "&gt;", $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

  • 写回答

1条回答 默认 最新

  • doukeng7426 2019-05-16 07:12
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)