Ver Mensaje Individual
  #3 (permalink)  
Antiguo 21/01/2012, 05:28
edie8
 
Fecha de Ingreso: noviembre-2011
Mensajes: 516
Antigüedad: 13 años, 2 meses
Puntos: 10
Respuesta: json en php como usar

es que estoy mirando este tutorial y me salen asi mira
index.html
Código PHP:
<html>
<
head>
<
title>Problema</title>
<
script src="funciones.js" language="JavaScript"></script>
</head>
<body>
<h2>Recuperar datos del servidor almacenados en una base de datos en formato JSON utilizando la librería JSON.php</h2>
<br>
<input type="button" value="Recuperar" id="boton1">
<div id="resultados"></div>
</body>
</html> 
funciones.js
Código PHP:
addEvent(window,'load',inicializarEventos,false);

function 
inicializarEventos()
{
  var 
ob=document.getElementById('boton1');
  
addEvent(ob,'click',presionBoton,false);
}

var 
conexion1;
function 
presionBoton(e)
{
  
conexion1=crearXMLHttpRequest();
  
conexion1.onreadystatechange procesarEventos;
  
conexion1.open('GET','pagina1.php'true);
  
conexion1.send(null);
}

function 
procesarEventos()
{
  var 
resultados document.getElementById("resultados");
  if(
conexion1.readyState == 4)
  {
    
alert('Cadena en formato JSON:  '+conexion1.responseText);

    var 
datos=eval("(" conexion1.responseText ")");
    var 
salida='';
    for(
f=0;f<datos.length;f++)
    {
      
salida += 'Codigo:'+datos[f].codigo+"<br>";
      
salida += 'Descripcion:'+datos[f].descripcion+"<br>";
      
salida += 'Precio:'+datos[f].precio+"<br><br>";
    }
    
resultados.innerHTML salida;
  } 
  else 
  {
    
resultados.innerHTML "Cargando...";
  }
}

//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (
elemento.attachEvent)
  {
    
elemento.attachEvent('on'+nomevento,funcion);
    return 
true;
  }
  else  
    if (
elemento.addEventListener)
    {
      
elemento.addEventListener(nomevento,funcion,captura);
      return 
true;
    }
    else
      return 
false;
}

function 
crearXMLHttpRequest() 
{
  var 
xmlHttp=null;
  if (
window.ActiveXObject
    
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (
window.XMLHttpRequest
      
xmlHttp = new XMLHttpRequest();
  return 
xmlHttp;

pagina1.php
Código PHP:
<?php
$conexion
=mysql_connect("localhost","root","z80") or die("Problemas en la conexion");
mysql_select_db("bdajax",$conexion) or die("Problemas en la seleccion
de la base de datos"
);
$registros=mysql_query("select codigo,descripcion,precio from perifericos",$conexion) or die("Problemas en el select".mysql_error());

while (
$reg=mysql_fetch_array($registros))
{
  
$vec[]=$reg;
}

require(
'../JSON.php');
$json=new Services_JSON();
$cad=$json->encode($vec);
echo 
$cad;
?>
En este ultimo agrega un require(JSON.php); que no se que es y no viene explicado la pagina es ajax ya nose si la conocen. Entonces se puede hacer lo mismo con el Json_encode??? gracias un saludo.