Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/06/2012, 15:23
Avatar de marlanga
marlanga
 
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 3 meses
Puntos: 206
Respuesta: imprimir valores desde un php a un html

En el PHP, codifica la respuesta en JSON. Por ejemplo, mete todos los datos en un array. Es muy importante que ese PHP NO IMPRIMA NADA en el documentos, sólo la última linea debe escribir.
Ni warnings ni nada, o no funcionará.

Código PHP:
Ver original
  1. <?php
  2. //tu codigo de crear los objetos esos...
  3. //NO DEBEN ESCRIBIR NADA en el documento
  4. $datos=array(
  5.     "hola" =>  $_GET['mensaje'],
  6.     "bus" =>  $usu_urg -> buscar($hola),
  7.     "bus2" => $dato_usu -> setUsuario($bus['out_usu_id']),
  8.     "id" =>  $dato_usu -> getFunID(),
  9.     "bus3" => $funcio -> setFuncionario($id),
  10.     "nombres" => $funcio -> getNombres()
  11. );
  12. echo json_encode($datos);
  13. ?>

Luego, en la página que hace la petición, recibe esos argumentos:
Código Javascript:
Ver original
  1. <html>
  2.     <head>
  3.         <script type="text/javascript" src="../plugins/jquery-1.7.2.js" ></script>
  4.     </head>
  5.  
  6.     <body>
  7.         <br>
  8.         <label>Nombre de Usuario:</label>
  9.         <input type="text" id="texto" />
  10.         <input  type= "button" value = "Consultar" id = "boton" />
  11.         <br></br>
  12.         <br></br>
  13.         <br></br>
  14.         <label>DATOS USUARIO</label>
  15.         <div id = "datos_usu" >
  16.             <br></br>
  17.             <br></br>
  18.             <br></br>
  19.         </div>
  20.         <label>PERMISOS QUE POSEE</label>
  21.         <div id ="permiso">
  22.            
  23.          
  24.    
  25.          
  26.         <br></br>
  27.         </div>
  28.        
  29.         <script>
  30.             $(document).ready(function(e) {
  31.                 $('#boton').click(function(){
  32.                     var mensaje = $('#texto').val();
  33.                    
  34.                     $.ajax({
  35.                         type     : "GET",
  36.                         url      : "admin_urgencia/ajax_admin_urg_add.php",
  37.                         dataType: 'json',
  38.                         data     : "mensaje="+mensaje,
  39.                         cache    : false,
  40.                         success  : function(datos){
  41.                                     alert(datos.hola);
  42.                                     alert(datos.nombres);  
  43.                         }
  44.  
  45.                     });
  46.                 });
  47.             });
  48.         </script>
  49.     </body>
  50. </html>