Jump to content

AJAX shoutbox not working in IE


robs99

Recommended Posts

Hey,

 

i was wondering if anyone can help me out. I have a simple ajax shoutbox which works fine on firefox & chrome, but fails to send or receive data on IE. does anyone have any suggestions?

 

here's a live example, you'll see it's working fine on FF & chrome: http://www.it-leaked.com/ajaxsb.php

 

here is the code:

 


1    2    //start the session
3    session_start();
4    ?>
5    
6    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
7    <html xmlns="http://www.w3.org/1999/xhtml">
8    <head>
9    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10   <title>Shoutbox</title>
11   <style type="text/css">
12   body
13   	{
14   	font-family: Verdana;
15   	font-size: 7pt;
16   	}
17   .shout
18   	{
19   	margin: 5px;
20   	}
21   #scroll_box
22   	{
23   	overflow: auto;
24   	height: 120px;
25   	width: 460px;
26   	border: 1px solid #333;
27   	margin-top: 3px;
28   	}
29   </style>
30   <script type="application/javascript">
31   var xmlHttp;
32   
33   function getShouts()
34   	{ 
35   	xmlHttp = GetXmlHttpObject();
36   	if (xmlHttp == null)
37   		{
38   		alert("Could not create a XmlHttpObject");
39   		return;
40   		}
41   	//set the get url var
42   	var url="shoutbox.php?q=get";
43   	xmlHttp.onreadystatechange = stateChanged;
44   	
45   	//open the $_GET['get'] page
46   	xmlHttp.open("GET",url,true);
47   	xmlHttp.send(null);
48   	}
49   	
50   function insertShout()
51   	{
52   	xmlHttp2 = GetXmlHttpObject();
53   	if (xmlHttp2 == null)
54   		{
55   		alert("Could not create a XmlHttpObject");
56   		return;
57   		}
58   	if (document.getElementById("shout").value == "" 
59   	|| document.getElementById("shout").value == null)
60   		{
61   		alert("ERROR: No shout entered.");
62   		return;
63   		}
64   	//set the get url var
65   	var shout = document.getElementById("shout").value;
66   	var url = "shoutbox.php?q=insert&shout="+shout;
67   	xmlHttp2.onreadystatechange = stateChanged;
68   	
69   	//open the $_GET['get'] page
70   	xmlHttp2.open("GET",url,true);
71   	xmlHttp2.send(null);
72   	
73   	getShouts();
74   	document.getElementById("shout").value = "";
75   	}
76   
77   function stateChanged() 
78   	{ 
79   	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
80    		{
81   		//fill the div scroll box with the formatted shouts or "xmlHttp.responseText"
82    		document.getElementById("scroll_box").innerHTML = xmlHttp.responseText;
83   		} 
84   	}
85   
86   function GetXmlHttpObject()
87   	{
88   	var xmlHttp = null;
89   	try
90   		{
91   		//Firefox, Opera 8.0+, Safari
92   		xmlHttp = new XMLHttpRequest();
93   		}
94   	catch (e)
95   		{
96   		//Internet Explorer
97   		try
98   			{
99   			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
100  			}
101  		catch (e)
102  			{
103  			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
104  			}
105  		}
106  	return xmlHttp;
107  	}
108  
109  //get the shouts for when the box is loaded initially
110  getShouts();
111  
112  function runme()
113  	{
114  	getShouts();
115  	setTimeout("runme()",3000);
116  	}
117  	
118  setTimeout("runme()",3000);
119  </script>
120  </head>
121  
122  <body>
123  
124  125  
126  //mysql info
127  $db_host = "REMOVED";
128  $db_name = "REMOVED";
129  $db_username = "REMOVED";
130  $db_password = "REMOVED";
131  
132  //connect to the database
133  $conn = mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
134  mysql_select_db($db_name, $conn) or die(mysql_error());
135  
136  //set the timezone for the script
137  putenv("TZ=America/Los_Angeles");
138  
139  //number of shouts to display
140  $i = 50;
141  
142  //function to query the database for the shouts. only parameter is amount of shouts
143  function get_shouts($i)
144  	{
145  	//the sql query to pull all the info from table `shoutbox`
146  	$sql = mysql_query("SELECT * FROM 2 ORDER BY id DESC LIMIT $i");
147  	
148  	//while there are still rows to assign to the $row var, do it
149  	while ($row = mysql_fetch_array($sql))
150  		{
151  		//set the row vars
152  		$user = $row['user'];
153  		$shout = $row['shout'];
154  		$time = $row['timestamp'];
155  		
156  		//echo the formatted shouts
157  		echo "<div class=\"shout\">".$user." [".$time."]<br />".htmlentities($shout)."</div>\n";
158  		}
159  	}
160  	
161  //function to insert the shouts
162  function insert_shout($user,$shout,$time)
163  	{
164  	$shout = mysql_real_escape_string($shout);
165  	mysql_query("INSERT INTO 2 (user,shout,timestamp) VALUES ('$user','$shout','$time')");
166  	}
167  
168  //url system for $_GET querys
169  if ($_GET['q'] == "archive")
170  	{
171  	//archive page
172  	echo "<!--archive-->\n\n";
173  	}
174  elseif ($_GET['q'] == "get")
175  	{
176  	//get query
177  	echo "<!--get-->\n\n";
178  	
179  	//get the formatted shouts while var $i is the amount of shouts to display
180  	get_shouts($i);
181  	}
182  elseif ($_GET['q'] == "insert")
183  	{
184  	//insert query
185  	echo "<!--insert-->\n\n";
186  	
187  	//get the username, shout and set the timestamp and then insert it all into the database
188  	insert_shout($_SESSION['user'],$_GET['shout'],date("g:i a"));
189  	}
190  else
191  	{
192  	//default page
193  	echo "<!--default-->\n\n";
194  	
195  	if (isset($_SESSION['user']))
196  		{
197  		echo "Welcome, ".$user = $_SESSION['user'];
198  		}
199  	else
200  		{
201  		$_SESSION['user'] = "Guest";
202  		echo "Welcome, ".$user = $_SESSION['user'];
203  		}
204  	
205  	//echo the form and fill the div scroll box with the formatted shouts
206  	echo "<br /><input type=\"text\" size=\"55\" id=\"shout\" />
207  	<input type=\"button\" value=\"Submit Query\" onclick=\"insertShout();\" />
208  	<div id=\"scroll_box\"></div>";
209  	}
210  
211  ?>
212  
213  </body>
214  </html>

 

 

I hope someone can help me :)

 

thanks in advance

Link to comment
Share on other sites

  • 6 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.