狐狸.fox 2015-08-26 18:32 采纳率: 0%
浏览 20

使用原型的Ajax请求

I am making a simple chat.

I am trying to send both message and chatid with the ajax function. But the problem is that the message is the only one sending.

I am using "prototype" for ajax (http://prototypejs.org/)

The form submits to a comet function, like this.

From:

<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>

To:

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

Then to a backend PHP file where it uses the GET function to collect the data.

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

The message passes though fine, but when I try to do the same with room:

$room = $_GET['room'];

I get "null"

Have been pulling my hair out for the past hours!

Full code:

 <?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>

backend.php

$filename  = dirname(__FILE__).'/data.txt';
$room = $_GET['room'];
// store new message in the file
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
  file_put_contents($filename,$msg);
  die();
}

// infinite loop until the data file is not modified
$lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
  usleep(10000); // sleep 10ms to unload the CPU
  clearstatcache();
  $currentmodif = filemtime($filename);
}
// return a json array
$response = array();
$response['msg']       = file_get_contents($filename);
$response['room']      = $room;
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
  • 写回答

0条回答 默认 最新

    报告相同问题?