Jump to content


leoni25

Member Since 20/11/2005
Offline Last Active 10/01/2011, 12:16
-----

Topics I've Started

Noticias Ajax + Php

04/01/2011, 14:21

Olá amigos,

Bem sou inciante em ajax, estava lendo uns tutoriais, e estou com um projeto de montar um sistema de noticias.

Montei um script no ajax que era pra funcionar da seguinte forma:
a função noticia apenas pega a o conteudo da noticia e adiciona na div noticias!
a função exibir pega o inicio e fim (resumindo quantas noticias vou exibir) faz uma verificação pra ver se existe algo dentro da div noticias se existir retira e depois faz um loop usa a função noticia para inserir as noticias na div.

Só que isso não acontece...
A função noticia funciona numa boa...o problema é o loop alguem poderia me ajudar ?

Codigo:

function criaXMLHttp() {
 if (typeof XMLHttpRequest != "undefined")
 	return new XMLHttpRequest();
 else if (window.ActiveXObject){
 	var versoes = ["MSXML2.XMLHttp.5.0",
 	"MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0",
 	"MSXML2.XMLHttp", "Microsoft.XMLHttp"
 	];
 }
 for (var i = 0; i < versoes.length; i++){
 	try{
 		return new ActiveXObject(versoes[i]);
 	}catch (e) {}
 }
 throw new Error("Seu browser nao suporta AJAX");
}

function noticia(id) {
		var noticia = document.getElementById("noticias");
		var XMLHttp = criaXMLHttp();
		XMLHttp.open("get", "lerid.php?id=" + id, true);
		XMLHttp.onreadystatechange = function () {
			if (XMLHttp.readyState == 4) {
				if (XMLHttp.status == 200) {
					noticia.innerHTML = noticia.innerHTML + "<br>" + XMLHttp.responseText;
				}else{
					noticia.innerHTML = "Ocorreu um erro: " + XMLHttp.statusText;
				}
			}
		}
		XMLHttp.send(null);
}

function exibir(inicio,fim) {
	var noticia = document.getElementById("noticias");
	fim = fim + 1;
	if (noticia.innerHTML != "") {
		noticia.innerHTML = "";
	}
	
	for (var i = inicio;i < fim;i++) {
		noticia(i);
	}
}

IPB Skin By Virteq