Jump to content

Help with AJAX / PHP chat


Bigg

Recommended Posts

Hey I'm trying to make a chat using php and AJAX that doesn't use a database and instead uses a form of server push to get messages from clients.  Here's what I got so far.

 

<html>
<body>

<script type="text/javascript">

function Makesobject()
{
if (window.XMLHttpRequest) {
				return new XMLHttpRequest();
			} else if(window.ActiveXObject) {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				document.getElementById('g').innerHTML = 'Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.';
			}
		}



function ajaxFunction() {
  var xmlHttp = Makesobject()
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.getElementById("echoed").value=unescape(xmlHttp.responseText);
        }
      }
  
    

//alert ("1.php?message="+document.getElementById("2").value+"&username="+document.getElementById("ur").value)
xmlHttp.open("GET","1.php?message~"+document.getElementById("2").value+"&username~ : "+document.getElementById("ur").value,true);


xmlHttp.send(null);
  }
</script>
<form name="myForm">
Name: <input type="text" name="message" id="2" >
<P id="g"></P>
Message: 
  <input type="text" name="username" id="ur" >

<P>
  <textarea name= "echoed" cols="35" rows="8" id="ec" col="40"> </textarea>  
  <input type="button" onClick="ajaxFunction()" value="Submit">
</form>

</body>
</html>

And the 1.php is

<?php
//echo $_SERVER['QUERY_STRING'];
$outerarray = explode("&", $_SERVER['QUERY_STRING']);
$that = explode ("~",$outerarray[0]);

echo $that[1];

$that = explode ("~",$outerarray[1]);

echo $that[1];

?>

Does anybody have any ideas for the rest as to the server push or like timeout to the server for messages?  I'm still a little new to php so please explain your code step by step.

Link to comment
https://forums.phpfreaks.com/topic/47305-help-with-ajax-php-chat/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.