使用Prototype的Ajax请求

时间:2015-08-26 18:32:44

标签: javascript php ajax

我正在进行简单的聊天。

我正在尝试使用ajax函数发送消息和chatid。 但问题是该消息是唯一发送的消息。

我正在使用"原型" for ajax(http://prototypejs.org/

表单提交给彗星函数,就像这样。

自:

@Point1.STUnion(@Point2).STConvexHull()

要:

<form action="" method="get" onsubmit="comet.doRequest($('word<?php echo $chatid?>').value, <?php echo $chatid?>);$('word<?php echo $chatid?>').value='';return false;">    
    <input type="text" name="word<?php echo $chatid?>" id="word<?php echo $chatid?>" value="" autocomplete="off"/>
    <input type="submit" name="submit" value="Send" />
</form>

然后到后端PHP文件,在那里它使用GET函数来收集数据。

   doRequest: function(request, request2)
  {
    new Ajax.Request(this.url, {
      method: 'get',
      parameters: { 'msg' : request, 'room' : request}
    }
    );
  }

消息通过虽然很好,但是当我尝试对房间做同样的事情时:

$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
  file_put_contents($filename,$msg);
  die();
}

我得到&#34; null&#34;

过去几个小时一直把头发拉出来!

完整代码:

$room = $_GET['room'];

backend.php

 <?php $chatid = 7 ?>
<form action="" method="get" onsubmit="comet.doRequest($('word<?php echo $chatid?>').value, <?php echo $chatid?>);$('word<?php echo $chatid?>').value='';return false;">    
            <input type="text" name="word<?php echo $chatid?>" id="word<?php echo $chatid?>" value="" autocomplete="off"/>
            <input type="submit" name="submit" value="Send" />
        </form>
                <script type="text/javascript">
                var Comet = Class.create();
                Comet.prototype = {

                  timestamp: 0,
                  url: './backend.php',
                  noerror: true,

                  initialize: function() { },

                  connect: function()
                  {
                    this.ajax = new Ajax.Request(this.url, {
                      method: 'get',
                      parameters: { 'timestamp' : this.timestamp },
                      onSuccess: function(transport) {
                        // handle the server response
                        var response = transport.responseText.evalJSON();
                        this.comet.timestamp = response['timestamp'];
                        this.comet.handleResponse(response);
                        this.comet.noerror = true;
                      },
                      onComplete: function(transport) {
                        // send a new ajax request when this request is finished
                        if (!this.comet.noerror)
                          // if a connection problem occurs, try to reconnect each 5 seconds
                          setTimeout(function(){ comet.connect() }, 5000); 
                        else
                          this.comet.connect();
                        this.comet.noerror = false;
                      }
                    });
                    this.ajax.comet = this;
                  },

                  disconnect: function()
                  {
                  },

                  handleResponse: function(response)
                  {
                    $('content').innerHTML += '<span id="msg">' + response['msg'] + response['room'] +'</span><br>';
                  },
                doRequest: function(request, request2)
                  {
                    new Ajax.Request(this.url, {
                      method: 'get',
                      parameters: { 'msg' : request, 'room' : request}
                    }
                    );
                  }
                }
                var comet = new Comet();
                comet.connect();
                </script>

0 个答案:

没有答案