Web Shoutbox Idea

Web Shoutbox Idea

本文关键字:Idea Shoutbox Web      更新时间:2023-09-26

你好,我正在考虑为我的网站制作一个shoutbox。我不想使用任何其他的,因为它不太适合我之前存在的成员数据库。我想了一些主意,但我真的不确定有什么更好的方法。我想提交一份表格,在没有"获取"的情况下发送一条消息。我也无法重新加载页面。这就是AJAX的用武之地:p

我想把我网页上的表格设置为:

<form method="post" onsubmit="return sendShout()" >
    <input type="text" name="Shout" id="Shout" />
</form>

我的javascript如下:

<script>
    function sendShout()
    {
     if(ShoutTime == 0)
     {
      var http = new XMLHttpRequest();
      http.open("GET", location.href+"?shout="+encodeURIComponent(document.getElementById("Shout").value)+"&name="+encodeURIComponent(document.getElementById("username").value), true);
      http.send();
      ShoutTime = <?php echo $shoutWait;?>+1;
      ShoutWait();
      unidle();
      document.getElementById("Shout").value='';
     }
     else
     {
      ShoutWaitNote();
      getLogs();
     }
     return false;
    }
</script>

然后在页面上,我可以像$_GET["out"]一样放入数据库。。。等

现在,有没有一种更好的方法可以使用ajax向mysql数据库发送呐喊,而不需要在url中将呐喊作为GET?

我怀疑这里还有更大的问题,但您可以使用XMLHttpRequest进行POST,如下所示:

var http = new XMLHttpRequest();
http.open("POST", location.href);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send("shout=something&name=something");

与GET版本相反:

var http = new XMLHttpRequest();
http.open("GET", location.href + "?shout=something&name=something");
http.send();

在这两种情况下,您都需要应用URL编码。祝你好运