Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/08/2010, 13:38
Avatar de intiweb
intiweb
 
Fecha de Ingreso: mayo-2009
Ubicación: Capital Federal
Mensajes: 363
Antigüedad: 14 años, 11 meses
Puntos: 5
Respuesta: problemas con codigo ajax

mira ahoa cambie los codigos por estos porque como nadie contestaba bueno algo fui probando:

codigo ajax
Código:
function getXMLHTTPRequest (){
try {
req = new XMLHttpRequest();
}catch(err1){
       try {
	     req = new ActiveXObject("Msxml2.XMLHTTP");
	   }catch(err2){
	      try { req = new ActiveXObject("Microsoft.XMLHTTP");
	   }catch(err3){
	    req = false;
	  }
    } 
  }
  return req;
}

var miPeticion = getXMLHTTPRequest ();
var http = getXMLHTTPRequest ();

function prevxfoto()
{
var ids = document.miform.namefoto.value;
var miAleatorio=parseInt(Math.random()*99999999);
var url = "servidorfotoprev.php?id=" + ids;
miPeticion.open("GET", url+ "&rand=" + miAleatorio, true);
miPeticion.onreadystatechange = respuestaAjaxFoto;
miPeticion.send(null);
}
function nexfoto()
{
var ids = document.miform.namefoto.value;
var miAleatorio=parseInt(Math.random()*99999999);
var url = "servidorfoto.php?id=" + ids;
miPeticion.open("GET", url+ "&rand=" + miAleatorio, true);
miPeticion.onreadystatechange = respuestaAjaxFoto;
miPeticion.send(null);
}

function respuestaAjaxFoto() {
if(miPeticion.readyState == 4) {
if(miPeticion.status == 200) {
 var datolink = miPeticion.responseXML.getElementsByTagName("link")[0];
 var datonamefoto = miPeticion.responseXML.getElementsByTagName("fotoname")[0];
 var datoid = miPeticion.responseXML.getElementsByTagName("id")[0];
 var textolink = datolink.childNodes[0].nodeValue;
 var textoid = datoid.childNodes[0].nodeValue;
 var textonamefoto = datonamefoto.childNodes[0].nodeValue;
 document.getElementById('foto').innerHTML = ("<a href=\"javascript:;\" onclick=\"nexfoto();\"><img src=\"" + textolink + "\" width=\"600\"></img></a>");			
 document.getElementById('namefoto').innerHTML = ("<input type=\"hidden\" id=\"namefoto\" value=\"" + textoid + "\"></form>");
 document.getElementById('actual').innerHTML = (textonamefoto);
 document.getElementById('nombrefoto').innerHTML = (textonamefoto);
			


        } else {
		alert("Ha ocurrido un error: " + miPeticion.statusText);
        }
    } else {     // si readyState ha cambiado
                 // pero readyState <> 4
           document.getElementById('foto').innerHTML = '<img src="jpg/cargador.jpg">';
    }

}

function comentarios() {

  var myurl = 'servidorcomentario.php?id=';
  var idss = document.miform.namefoto.value;
  var comentario = document.comentariofoto.comentario.value;
  var usuario = document.comentariofoto.username.value;
  
    myRand = parseInt(Math.random()*999999999999999);
    // saltarse la cache
   
   var modurl = myurl+escape(idss)+"&comentario="+escape(comentario)+"&username="+escape(usuario)+"&rand="+myRand;
   http.open("GET", modurl, true);
   http.onreadystatechange = useHttpResponse;
   http.send(null);
}
function useHttpResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
       // primero elimina los nodos hijos
       // presentes en el contenedor div
       while (document.getElementById('newcomentary') .hasChildNodes())
      {
document.getElementById('newcomentary').removeChild(document .getElementById('newcomentary').firstChild);
      }
      var userNodes = http.responseXML .getElementsByTagName("usuario");
      var comentaryNodes = http.responseXML .getElementsByTagName("comentario");
      var fechaNodes = http.responseXML .getElementsByTagName("fecha");
	  var horaNodes = http.responseXML .getElementsByTagName("hora");
	  
      for(var i =1;i<userNodes.length;i++)
      {
        var newuser = document .createTextNode(userNodes[i] .childNodes[0].nodeValue);
        var newpara = document.createElement('p');
        var para = document.getElementById('newcomentary') .appendChild(newpara);
        newpara.appendChild(newuser);
        

        var newtext2 = document .createTextNode(comentaryNodes[i] .childNodes[0].nodeValue);
        var newpara2 = document.createElement('p');
        var para2 = document .getElementById('newcomentary').appendChild(newpara2);
        newpara2.appendChild(newtext2);
        
		
        var newtext3 = document .createTextNode(fechaNodes[i] .childNodes[0].nodeValue);
        var newpara3 = document.createElement('p');
        var para3 = document.getElementById('newcomentary') .appendChild(newpara3);
        newpara3.appendChild(newtext3);
        
		
		var newtext4 = document .createTextNode(horaNodes[i] .childNodes[0].nodeValue);
        var newpara4 = document.createElement('p');
        var para3 = document.getElementById('newcomentary') .appendChild(newpara4);
        newpara4.appendChild(newtext4);
        
      }
    }
  }
}
y el servidor php tiene este codigo:
Código PHP:
include("fns.php");
include("loops.php");

header('Content-Type: text/xml');
echo "<?xml version="1.0\" ?>";
echo 
"<comentarios>";

        
$db_conn db_connect();

     
$username $_GET['username'];
     
$id $_GET['id'];
     
$comentario $_GET['comentario'];
     
$year date("Y");
     
$mes date("m");
     
$dia date("d");
     
$hora date("H");
     
$minuto date("i");
     
$segundo date("s");
     
     
$insertcomentario "insert into comentarios_fotos values ('".$id."', '".$username."', '".$comentario."', '".$year."', '".$mes."', '".$dia."', '".$hora."', '".$minuto."', '".$segundo."')";
     
$resultinsert mysql_query($insertcomentario$db_conn) or die (mysql_error());
     
     
$querycomentario "select * from comentarios_fotos where id = '$id'";
     
$resultcomentario mysql_query ($querycomentario$db_conn) or die (mysql_error());
     
$numcomentario mysql_num_rows($resultcomentario);
     
     if (
$numcomentario>0)
     {
          while (
$array mysql_fetch_array($resultcomentario))
          {
             
$user $array['usuario'];
             
$comentario $array['comentario'];
             
$fecha $array["dia"]."-".$array["mes"]."-".$array["year"];
             
$hora $array["hora"].":".$array["minuto"].":".$array["segundo"]." Hs:";
             
             echo 
"<comen>
                   <usuario>$user</usuario>
                   <comentario>$comentario</comentario>
                   <fecha>$fecha</fecha>
                   <hora>$hora</hora>
                   </comen>"
;  
          }
     }
     
     
        echo 
"</comentarios>";
al menos me muestra ol el nombre o el comentario no se bien cual de los dos pero en este codigo no esta formateado.

el error es el siguiente:

Error: comentaryNodes[i].childNodes[0] is undefined
Archivo de origen: http://localhost/municipios/fotos.ph...01971.JPG&id=8
Línea: 107

el sistema esta bien asi como lo quiero armar es un sistema donde hay una foto y la gente la comenta cuando envias el comentario lo carga y actualiza con ajax los anteriores comentarios y te los muestra formateados