Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Ajax Problem... kinda


  • Please log in to reply
4 replies to this topic

#1 KamakaZ

KamakaZ

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:05:44 AM

Posted 01 June 2009 - 10:12 AM

Hi guys, any ideas why this would update in FF but not in IE? The site is at http://202.45.110.174/social/test.

I know it's a big lump of code... sorry! It all works fine if i take the Ajax out and chuck in some php to grab the messages but i want it to auto update...

This has got me stumped...

EDIT: The original code i got from here http://woork.blogspot.com/2009/05/how-to-i...l-facebook.html (the site is a bit dodgy sometimes won't load).

The code i have is:

index.php
<html>

<head>

<title>B-rad's Wall</title>

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="stylesheet" type="text/css" href="css.css" />
&lt;script type="text/javascript" src="jquery/jquery-1.3.2.min.js"></script>

&lt;script type="text/javascript">
$(document).ready(function(){
	$("form#submit_wall").submit(function() {
 
	var message_wall = $('#message_wall').attr('value');
 
		$.ajax({
			type: "POST",
			url: "insert.php",
			data: "message_wall="+ message_wall,
			success: function(){
				$("ul#wall").prepend("<li style='display:none'>"+message_wall+"</li>");
				$("ul#wall li:first").fadeIn();
			}
		});
	return false;
	});
});
</script>

</head>


<body>
<h2>B-rad's Wall</h2>
<form action="" id="submit_wall">

<label for="message_wall">Your message:&nbsp;</label>

<input type="text" id="message_wall" class="input" value="Message - First Name" />
<button type="submit">Write!</button>

<button type="reset" onclick="window.location='http://202.45.110.174/social/test'">Refresh</button>

</form>

<table align="center" border="0" height="100%" width="100%">
<tr>
<td width="32%">

</td>
<td  width="53%">

<!--Ajax Start-->
&lt;script type="text/javascript">
var xmlhttp;

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("_ticker").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}


  var init_ticker = function(){

   xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="data.php";
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
xmlhttp.timeout = 10000
  };
  setInterval(init_ticker, 1000);
  window.onload = init_ticker;



</script>
<div class="_ticker" id="_ticker">Loading...</div>
<!--Ajax finish-->
</td>
<td width="15%">&nbsp;</td>
</tr> 
</table> 
<br />
<br />
<a href="http://www.redhost.com.au"><img src="redhost.jpg" align="center" border="0" /></a>
<h4>Affordable Web Hosting and Design</h4>
<h4>Email: Brad@redhost.com.au  .:. <a href="http://www.redhost.com.au">www.Redhost.com.au</a></h4>



</body>

</html>

data.php
<?
$db_host="localhost";

$db_name="messages";

$username="username";

$password="*******";

$db_con=mysql_connect($db_host,$username,$password);

$connection_string=mysql_select_db($db_name);

mysql_connect($db_host,$username,$password);

mysql_select_db($db_name);


$result = mysql_query("select * from messages ORDER BY ID desc");
if(!$result) echo mysql_error();
else {
while($row = mysql_fetch_row($result)) {
?>
<ul id="wall">
<li>
<?
echo "$row[1]<br />\n";
?>
</li>
</ul>
<?
}
}
?>

and the two javascripts up in the index.php page use this to insert the message into the database

insert.php
<?php
if(isset($_POST['message_wall'])){
/* Connection to Database */
include('config.php');
/* Remove HTML tag to prevent query injection */
$message = strip_tags($_POST['message_wall']);

$sql = 'INSERT INTO messages (messages) VALUES( "'.$message.'")';
mysql_query($sql);

echo $message;


} else { echo '0'; }
?>

Edited by KamakaZ, 01 June 2009 - 10:15 AM.

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:44 PM

Posted 01 June 2009 - 11:56 AM

Sort of hard to say. That is where debugging comes in. :thumbsup:

#3 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:05:44 AM

Posted 02 June 2009 - 03:56 AM

I have tried various things, such as removing doctype and other snippets of code... where would you start?

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:44 PM

Posted 02 June 2009 - 09:49 AM

With a debugger and stepping through code. Firebug, whatever.

Yahoo has an AJAX library and tutorials that are much easier to handle than what you are doing. They take care of all of the browser inconsistencies, plus the tutorial is really good. I am using it for my AJAX requests, and once you understand what is happening, it is dead easy to use:
http://developer.yahoo.com/yui/connection/

Here is my generic AJAX call from the project that I am working on:
function ajaxCall(url,obj, handleSuccess){

					var handleFailure = function(o){
						obj.innerHTML = 'Ajax load error' + url + '\n' + o;
					};

					var callback = { success: handleSuccess, failure: handleFailure };
					YAHOO.util.Connect.asyncRequest('GET', url, callback);
				}

It gets three parameters; the type of request (which is a GET, the url, and a callback object). The callback object is already defined. If you notice in the parameters being passed to the ajaxCall function, one of them is an obj, and the other is a handleSuccess. This is where your function that uses ajaxCall tells what to do with the returned object. For instance, here is how I am getting objects to populate a menu:
function loadMenu(){
					var url = urlBase + "extractsUtility?switch=0";
					var obj = document.getElementById("menuContainer");

					var handleSuccess=function(o){

						try{
							var menu =  YAHOO.lang.JSON.parse(o.responseText);
							buildExtractMenu(menu);
						}catch(e){
							obj.innerHTML = 'Failure processing menu items ' + url + '\n\n' + e;
						}
					};
					ajaxCall(url,obj, handleSuccess);
				}

You can see in the middle there that there is an inner function called handleSuccess (which gets passed to the ajaxCall function). What it is saying is that once I get my object back, then I pass it to another function that actually builds the menu.

This is just one way of doing things. I could actually have the handleSuccess function inside the AJAX call if I was only using it for one thing, but since I am using it to handle multiple requests, it makes sense to make the AJAX call as generic as possible.

See where that gets you. What I would recommend doing is starting a test project to work through the tutorial, and see what you can figure out.

#5 KamakaZ

KamakaZ
  • Topic Starter

  • Members
  • 739 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Victoria
  • Local time:05:44 AM

Posted 03 June 2009 - 07:26 PM

wow a generic AJAX call would be great! i'll have a look through it and also look into firebug or something similar... i'll keep you posted!

There's no place like 127.0.0.1
There are 10 types of people in the world, those that can read binary, and those who can't.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users