Ver Mensaje Individual
  #7 (permalink)  
Antiguo 04/05/2011, 08:50
Avatar de chicohot20
chicohot20
 
Fecha de Ingreso: mayo-2009
Mensajes: 388
Antigüedad: 15 años
Puntos: 43
Respuesta: Problema: variable fuera de una función ajax

Como haces una petición ajax, el valor que debe tomar la variable tmp tardará un poco, por que el servidor no te responderá inmediatamente. Pero si le pones el alert despues de settmp(Comprobacion); verás que si te imprime. Yo lo hice de otra manera, te dejo el codigo:
HTML
Código HTML:
Ver original
  1.     <head>
  2.         <title>Ejemplo</title>
  3.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  4.         <script>
  5.         var tmp=null;
  6.         function settmp(data){
  7.             tmp = data;
  8.         };
  9.         function chkform(){
  10.                 $.ajax({
  11.                   url: 'ajax.php',
  12.                   data: 'doc=c',
  13.                   type: 'POST',
  14.                   contentType: "application/x-www-form-urlencoded; charset=utf-8",
  15.                   success: function(data){
  16.                     settmp(data);
  17.                     alert(tmp);
  18.                     $("#resultado").text("Dato recibido: "+data);
  19.                   }
  20.                 });
  21.         }
  22.         chkform();
  23.        
  24.     </script>
  25. </head>
  26.  <p id="resultado"><img src='load.gif' alt='' /> Cargando...</p>
  27.  
  28. </body>
  29. </html>

ajax.php
Código PHP:
Ver original
  1. <?
  2.     $boton=$_POST["doc"];          
  3.     echo $boton;
  4. ?>

Coméntanos si te funciona.