Foros del Web » Programando para Internet » Javascript » Frameworks JS »

Enviar variables con ajax

Estas en el tema de Enviar variables con ajax en el foro de Frameworks JS en Foros del Web. Hola amig@s, (NO SE NADA DE AJAX SEGUN LO QUE HE LEIDO SE PUEDE HACER CON AJAX POR ESO VENGO A PREGUNTAR)( para los que ...
  #1 (permalink)  
Antiguo 07/07/2010, 23:42
 
Fecha de Ingreso: enero-2010
Mensajes: 113
Antigüedad: 14 años, 3 meses
Puntos: 2
Exclamación Enviar variables con ajax

Hola amig@s,

(NO SE NADA DE AJAX SEGUN LO QUE HE LEIDO SE PUEDE HACER CON AJAX POR ESO VENGO A PREGUNTAR)(para los que les molesta las mayusculas disculpen) bueno busco hacer lo siguiente:

tengo un formulario:

Código HTML:
Ver original
  1. <form action="" method="POST">
  2. nombre: <input name="nombre" type="text" value="1"/></br>
  3. apellido: <input name="apellido" type="text" value="1"/></br>
  4. <input type="submit" value="Enviar"/>
  5. <input type="button" value="Automatico"/>
  6. </form>

el formulario funciona de esta manera:

Al hacer click en Automatico genera el nombre y apellido luego al hacer click en enviar ya se envia el formulario para cumplir su respectiva funcion.

el archivo php puede ser algo asi:

Código PHP:
Ver original
  1. <?php
  2. $apuno="apellido 1";
  3. $nomuno="nombre 1";
  4. $apdos="apellido 2";
  5. $nomdos="nombre 2";
  6. $apellido=$_POST["apellido"];
  7. $nombre=$_POST["nombre"];
  8. if($nombre==1 || $apellido==1) {
  9.     $apellido=$apuno;
  10.     $nombre=$nomuno;
  11.     }
  12. else{
  13.     $nombre=$nomdos;
  14.     $apellido=$apdos;
  15.     }
  16. ?>

necesito que ajax haga un llamado al archivo php y recoja el valor de nombre y apellido y los coloque en el value del respectivo campo.

Chao espero puedan ayudarme muchas gracias.
  #2 (permalink)  
Antiguo 08/07/2010, 07:26
 
Fecha de Ingreso: enero-2010
Mensajes: 113
Antigüedad: 14 años, 3 meses
Puntos: 2
Respuesta: Enviar variables con ajax

Alguien que me ayude ?

almenos diganme si no se puede para dejar de buscar.
  #3 (permalink)  
Antiguo 08/07/2010, 11:01
 
Fecha de Ingreso: julio-2010
Mensajes: 14
Antigüedad: 13 años, 9 meses
Puntos: 1
Respuesta: Enviar variables con ajax

yo te recomendaria jquery pero hay infinidad de funciones que te permiten usar ajax pero como te digo yo uso y te recomiendo ampliamente [URL="http://jquery.com"]jquery[/URL] descargas el archivo lo agregas a tu pagina y haces lo siguiente:


Código Javascript:
Ver original
  1. <script>
  2. function IrPorAutoNombreApellido(){
  3. $.ajax({
  4.    type: "POST",
  5.    url: "yourfile.php",
  6.    data: "nombre=John&apellido=Boston",
  7.    success: function(respServ){
  8.         //msg trai lo que  yourfile.php respondio osea que yourfile.php debe hacer solo  por decirlo haci un echo $nombre .'|'.$apellido; entonces tu haria lo siguiente
  9.  
  10.            documen.getElementById("nombre").value=respServ.split("|")[0];
  11.            documen.getElementById("apellidos").value=respServ.split("|")[1];
  12.  
  13.    }
  14.  });
  15. <script>
y listo espero y te sirva de algo . . .

Última edición por ecastaneda; 09/07/2010 a las 07:24
  #4 (permalink)  
Antiguo 08/07/2010, 12:35
 
Fecha de Ingreso: enero-2010
Mensajes: 113
Antigüedad: 14 años, 3 meses
Puntos: 2
Respuesta: Enviar variables con ajax

listo voy a probarlo muchas gracias :)
  #5 (permalink)  
Antiguo 08/07/2010, 14:46
 
Fecha de Ingreso: enero-2010
Mensajes: 113
Antigüedad: 14 años, 3 meses
Puntos: 2
Respuesta: Enviar variables con ajax

Bueno mira hice lo que dices:

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
  3. function buscar(){
  4. $.ajax({
  5.    type: "POST",
  6.    url: "canal.php",
  7.    data: "nombre=canino&apellido=latino",
  8.   success: function(respServ){
  9.        //msg trai lo que  yourfile.php respondio osea que yourfile.php debe hacer solo  por decirlo haci un echo $nombre .'|'.$apellido; entonces tu haria lo siguiente
  10.  
  11.            documen.getElementById("nombre").value=respServ.split("|")[0];
  12.            documen.getElementById("apellido").value==respServ.split("|")[0];
  13.  
  14.    }
  15.  });
  16. </head>
  17. <form action="" method="POST">
  18. nombre: <input name="nombre" id="nombre" type="text" value="1"/></br>
  19. apellido: <input name="apellido" id="apellido" type="text" value="1"/></br>
  20. <input type="submit" value="Enviar"/>
  21. <input type="button" value="Automatico" onclick="buscar();"/>
  22. </form>
  23. </body>
  24. </html>

y este es canal.php:

Código PHP:
Ver original
  1. <?php
  2. echo $_POST["nombre"]."|";
  3. echo $_POST["apellido"];
  4. ?>

y no funciona.

no entiendo porque.

Gracias espero puedas ayudarme.
  #6 (permalink)  
Antiguo 09/07/2010, 07:13
 
Fecha de Ingreso: julio-2010
Mensajes: 14
Antigüedad: 13 años, 9 meses
Puntos: 1
Respuesta: Enviar variables con ajax

que error te sale ???
  #7 (permalink)  
Antiguo 09/07/2010, 07:18
 
Fecha de Ingreso: julio-2010
Mensajes: 14
Antigüedad: 13 años, 9 meses
Puntos: 1
Respuesta: Enviar variables con ajax

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
  3. function buscar(){
  4. $.ajax({
  5.    type: "POST",
  6.    url: "canal.php",
  7.    data: "nombre=canino&apellido=latino",
  8.   success: function(respServ){
  9.        //msg trai lo que  yourfile.php respondio osea que yourfile.php debe hacer solo  por decirlo haci un echo $nombre .'|'.$apellido; entonces tu haria lo siguiente
  10.  
  11.            documen.getElementById("nombre").value=respServ.split("|")[0];
  12.            documen.getElementById("apellido").value=respServ.split("|")[1];
  13.  
  14.    }
  15.  });
  16. }
  17. </head>
  18. <form action="" method="POST">
  19. nombre: <input name="nombre" id="nombre" type="text" value="1"/></br>
  20. apellido: <input name="apellido" id="apellido" type="text" value="1"/></br>
  21. <input type="submit" value="Enviar"/>
  22. <input type="button" value="Automatico" onclick="buscar();"/>
  23. </form>
  24. </body>
  25. </html>
haber usa este codigo le faltaba una llave

Última edición por ecastaneda; 09/07/2010 a las 07:24
  #8 (permalink)  
Antiguo 09/07/2010, 07:21
 
Fecha de Ingreso: julio-2010
Mensajes: 14
Antigüedad: 13 años, 9 meses
Puntos: 1
Respuesta: Enviar variables con ajax

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
  3. function buscar(){
  4.   $.ajax({
  5.      type: "POST",
  6.      url: "canal.php",
  7.      data: "nombre=canino&apellido=latino",
  8.     success: function(respServ){
  9.          //msg trai lo que  yourfile.php respondio osea que yourfile.php debe hacer solo  por decirlo haci un echo $nombre .'|'.$apellido; entonces tu haria lo siguiente
  10.  
  11.              documen.getElementById("nombre").value=respServ.split("|")[0];
  12.              documen.getElementById("apellido").value=respServ.split("|")[1];
  13.      
  14.      }
  15.   });
  16. }
  17. </head>
  18. <form action="" method="POST">
  19. nombre: <input name="nombre" id="nombre" type="text" value="1"/></br>
  20. apellido: <input name="apellido" id="apellido" type="text" value="1"/></br>
  21. <input type="submit" value="Enviar"/>
  22. <input type="button" value="Automatico" onclick="buscar();"/>
  23. </form>
  24. </body>
  25. </html>
haber cala ese le hacia falta una llave . . .
  #9 (permalink)  
Antiguo 10/07/2010, 13:38
 
Fecha de Ingreso: enero-2010
Mensajes: 113
Antigüedad: 14 años, 3 meses
Puntos: 2
Respuesta: Enviar variables con ajax

Listo funciono aunque tenia un error le faltaba una T en document:

codigo corregido:

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
  3. function buscar(){
  4.   $.ajax({
  5.      type: "POST",
  6.      url: "canal.php",
  7.      data: "nombre=canino&apellido=latino",
  8.     success: function(respServ){
  9.          //msg trai lo que  yourfile.php respondio osea que yourfile.php debe hacer solo  por decirlo haci un echo $nombre .'|'.$apellido; entonces tu haria lo siguiente
  10.  
  11.              document.getElementById("nombre").value=respServ.split("|")[0];
  12.              document.getElementById("apellido").value=respServ.split("|")[1];
  13.      
  14.      }
  15.   });
  16. }
  17. </head>
  18. <form action="" method="POST">
  19. nombre: <input name="nombre" id="nombre" type="text" value="1"/></br>
  20. apellido: <input name="apellido" id="apellido" type="text" value="1"/></br>
  21. <input type="submit" value="Enviar"/>
  22. <input type="button" value="Automatico" onclick="buscar();"/>
  23. </form>
  24. </body>
  25. </html>

Muchas gracias.

Etiquetas: ajax, enviar, variables
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 22:45.