Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 网络Shoutbox理念_Php_Javascript_Ajax - Fatal编程技术网

Php 网络Shoutbox理念

Php 网络Shoutbox理念,php,javascript,ajax,Php,Javascript,Ajax,你好,我正在考虑为我的网站制作一个shoutbox。我不想使用任何其他的,因为它不适合我现有的成员数据库。我想了一些主意,但我不确定有没有更好的办法。我想提交一份表格,但没有“GET”的情况下发出一声喊叫。我也无法重新加载页面。这就是AJAX的用武之地:p 我想在我的网页上设置以下表单: <form method="post" onsubmit="return sendShout()" > <input type="text" name="Shout" id="Shou

你好,我正在考虑为我的网站制作一个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>

函数sendShout()
{
如果(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=+1;
ShoutWait();
unidle();
document.getElementById(“Shout”).value='';
}
其他的
{
ShoutWaitNote();
getLogs();
}
返回false;
}
然后在页面上,我可以像$_GET['shout']一样放入数据库。。。等等


现在有没有更好的方法可以使用ajax向mysql数据库发送一条消息,而不必在url中使用消息?

我怀疑这里有更大的问题,但是您可以使用XMLHttpRequest这样做:

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编码。祝你好运。

你为什么不想使用GET?为此,首选POST方法。你不是要求它提供一些你想获得的数据,而是希望将一些数据发布到服务器上。因为在主页上,如果用户在url中添加了“#”,那么shoutbox会自动返回(因为?shout=不在那里。)@Fakerainbrigan是的,我想发布,但也不必重新加载页面。听起来你好像做错了什么。我建议使用一个专门用于shoutbox ajax api的PHP文件,而不是在内容页中包含shoutbox处理。我的感觉是事情变得有点混乱,导致混乱的局面。