Web Shoutbox创意

时间:2013-08-04 04:37:37

标签: php javascript ajax

您好我正在考虑为我的网站制作一个shoutbox。我不想使用任何其他因为它不适合我已有的成员数据库。我想到了一些想法,但我不确定更好的做法。我想提交一份表格而没有'GET'发送一声喊叫。我也无法重新加载页面。这就是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 ['shout']那样放入数据库......等等。

现在是否有更好的方法可以使用ajax将呼叫发送到mysql数据库而不必在网址中将呼叫作为GET?

1 个答案:

答案 0 :(得分:1)

我怀疑这里有更大的问题,但您可以像这样使用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();

在这两种情况下,您都希望应用网址编码。祝你好运。

相关问题