Foros del Web » Programando para Internet » PHP »

No se muestra datos php y ajax

Estas en el tema de No se muestra datos php y ajax en el foro de PHP en Foros del Web. Hola compañeros estoy queriendo aprender lo que es php con ajax, estoy siguiento este tutorial el cual muestra todos los pasos y los archivos de ...
  #1 (permalink)  
Antiguo 08/05/2013, 10:28
 
Fecha de Ingreso: septiembre-2010
Mensajes: 64
Antigüedad: 13 años, 7 meses
Puntos: 5
Pregunta No se muestra datos php y ajax

Hola compañeros estoy queriendo aprender lo que es php con ajax, estoy siguiento este tutorial el cual muestra todos los pasos y los archivos de hacerlos
Código:
http://www.tutorialspoint.com/php/php_and_ajax.htm
Pero al querer hacerlo correr en localhost no funciona nada ya cambie las conexiones y segun el tutorial deberia funcionar todo bien, cual puede ser el problema??
Mis archivos ajax.html
Código HTML:
<html>
<body>
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
 var ajaxRequest;  // The variable that makes Ajax possible!
	
 try{
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }catch (e){
   // Internet Explorer Browsers
   try{
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e) {
      try{
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function(){
   if(ajaxRequest.readyState == 4){
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.value = ajaxRequest.responseText;
   }
 }
 // Now get the value from user and pass it to
 // server script.
 var age = document.getElementById('age').value;
 var wpm = document.getElementById('wpm').value;
 var sex = document.getElementById('sex').value;
 var queryString = "?age=" + age ;
 queryString +=  "&wpm=" + wpm + "&sex=" + sex;
 ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
 ajaxRequest.send(null); 
}
//-->
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' />
<br />
Sex: <select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html> 
ajax-example.php
Código PHP:
<?php
$dbhost 
"localhost";
$dbuser "root";
$dbpass "vertrigo";
$dbname "ajax_buscador";
    
//Connect to MySQL Server
mysql_connect($dbhost$dbuser$dbpass);
    
//Select Database
mysql_select_db($dbname) or die(mysql_error());
    
// Retrieve data from Query String
$age $_GET['age'];
$sex $_GET['sex'];
$wpm $_GET['wpm'];
    
// Escape User Input to help prevent SQL Injection
$age mysql_real_escape_string($age);
$sex mysql_real_escape_string($sex);
$wpm mysql_real_escape_string($wpm);
    
//build query
$query "SELECT * FROM ajax_example WHERE sex = '$sex'";
if(
is_numeric($age))
    
$query .= " AND age <= $age";
if(
is_numeric($wpm))
    
$query .= " AND wpm <= $wpm";
    
//Execute query
$qry_result mysql_query($query) or die(mysql_error());

    
//Build Result String
$display_string "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

// Insert a new row in the table for each person returned
while($row mysql_fetch_array($qry_result)){
    
$display_string .= "<tr>";
    
$display_string .= "<td>$row[name]</td>";
    
$display_string .= "<td>$row[age]</td>";
    
$display_string .= "<td>$row[sex]</td>";
    
$display_string .= "<td>$row[wpm]</td>";
    
$display_string .= "</tr>";
    
}
echo 
"Query: " $query "<br />";
$display_string .= "</table>";
echo 
$display_string;
?>
  #2 (permalink)  
Antiguo 08/05/2013, 11:08
Avatar de repara2  
Fecha de Ingreso: septiembre-2010
Ubicación: München
Mensajes: 2.445
Antigüedad: 13 años, 7 meses
Puntos: 331
Respuesta: No se muestra datos php y ajax

Utiliza la consola de Firebug para obtener el error y desde allí puedes continuar.
Salu2
__________________
Fere libenter homines, id quod volunt, credunt.
  #3 (permalink)  
Antiguo 08/05/2013, 11:50
 
Fecha de Ingreso: septiembre-2010
Mensajes: 64
Antigüedad: 13 años, 7 meses
Puntos: 5
Respuesta: No se muestra datos php y ajax

Segun pude ver en modo consola si se procesan los datos y muestra los resultados, pero el problema es que no sale esos resultados en pantalla, cual podria ser el problema??
  #4 (permalink)  
Antiguo 08/05/2013, 14:48
 
Fecha de Ingreso: septiembre-2010
Mensajes: 64
Antigüedad: 13 años, 7 meses
Puntos: 5
Respuesta: No se muestra datos php y ajax

Habilite javascript modifique el codigo y nada, debe ser alguna pequeñez q no logro ubicarlo espero me brinden su ayuda porfavor
  #5 (permalink)  
Antiguo 08/05/2013, 16:02
 
Fecha de Ingreso: abril-2008
Ubicación: El Salvador
Mensajes: 736
Antigüedad: 16 años
Puntos: 47
Respuesta: No se muestra datos php y ajax

Mira ocupa librerias ya creadas facilitan mucho la vida...

No estoy muy familiarizado con esto

Código Javascript:
Ver original
  1. //Browser Support Code
  2. function ajaxFunction(){
  3.  var ajaxRequest;  // The variable that makes Ajax possible!
  4.    
  5.  try{
  6.    // Opera 8.0+, Firefox, Safari
  7.    ajaxRequest = new XMLHttpRequest();
  8.  }catch (e){
  9.    // Internet Explorer Browsers
  10.    try{
  11.       ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
  12.    }catch (e) {
  13.       try{
  14.          ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
  15.       }catch (e){
  16.          // Something went wrong
  17.          alert("Your browser broke!");
  18.          return false;
  19.       }
  20.    }
  21.  }
Pero con la librerias como jquery no necesitas andar evaluando nada de esto..

Además con una libreria hay muchas cosas prefabricadas y no necesitas reinventar la rueda..

http://docs.jquery.com/Tutorials#Tut...n_espa.C3.B1ol
  #6 (permalink)  
Antiguo 08/05/2013, 18:18
 
Fecha de Ingreso: septiembre-2010
Mensajes: 64
Antigüedad: 13 años, 7 meses
Puntos: 5
Respuesta: No se muestra datos php y ajax

Bueno gracias por todas las respuestas tendre q ver otras opciones

Etiquetas: ajax, html, muestra, mysql, select, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 11:17.