Foros del Web » Programando para Internet » Javascript »

LLamando a una funcion dentro de javascritp no funciona

Estas en el tema de LLamando a una funcion dentro de javascritp no funciona en el foro de Javascript en Foros del Web. Hola amigos, Vereis tengo una funcion y la quiero llamar desde el dentro las etiquetas javascritp nada especial la cosa es que no se por ...
  #1 (permalink)  
Antiguo 06/10/2015, 17:58
 
Fecha de Ingreso: marzo-2007
Mensajes: 751
Antigüedad: 17 años, 1 mes
Puntos: 4
LLamando a una funcion dentro de javascritp no funciona

Hola amigos,

Vereis tengo una funcion y la quiero llamar desde el dentro las etiquetas javascritp nada especial la cosa es que no se por que no está funcionado, esta es la funcion y la llamada enviarFormulario() :
Código Javascript:
Ver original
  1. function nuevoAjax(){
  2.  
  3.         var xmlhttp=false;
  4.         try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  5.         } catch (e) {try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  6.         } catch (E) {xmlhttp = false;}}
  7.         if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  8.         xmlhttp = new XMLHttpRequest();}
  9.         return xmlhttp;
  10.     }
  11.  
  12.  
  13. enviarFormulario("comparativas_select.php", "formulario_1", "resultado");
  14. enviarFormulario("comparativas_select.php", "formulario_2", "resultado2");
  15.  
  16.  
  17.     function enviarFormulario(url, formid, divrespuesta){
  18.             var Formulario = document.getElementById(formid);
  19.             var longitudFormulario = Formulario.elements.length;
  20.             var cadenaFormulario="";
  21.             for (var i=0; i <= Formulario.elements.length-1;i++) {
  22.             if(Formulario.elements[i].type=="checkbox" || Formulario.elements[i].type=="radio"){
  23.                 if(Formulario.elements[i].checked==true){cadenaFormulario += "&"+Formulario.elements[i].name+'='+encodeURIComponent(Formulario.elements[i].value);}
  24.             }else{cadenaFormulario += "&"+Formulario.elements[i].name+'='+encodeURIComponent(Formulario.elements[i].value);}
  25.             }
  26.  
  27. alert('buena');
  28.  
  29.  
  30.             peticion=nuevoAjax();
  31.             peticion.open("POST", url, true);
  32.             peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  33.             peticion.send(cadenaFormulario);
  34.             peticion.onreadystatechange = function() {
  35.                 if (peticion.readyState == 4 && (peticion.status == 200 || window.location.href.indexOf ("http") == - 1)){
  36.                     RESPUESTA=peticion.responseText;
  37.                         document.getElementById(divrespuesta).innerHTML =RESPUESTA;                  
  38.                 }
  39.             if (peticion.readyState == 4 && peticion.status != 200){
  40.                 document.getElementById(divrespuesta)="FALLA EN LA COMUNICACION... SI EL PROBLEMA CONTINUA, FAVOR DE REPORTARLO";
  41.             }
  42.             }
  43.  
  44.  
  45.         if(divrespuesta == 'resultado'){
  46.  
  47.             peticion0=nuevoAjax();
  48.             peticion0.open("POST", "comparativas_info1.php", true);
  49.             peticion0.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  50.             peticion0.send(cadenaFormulario);
  51.             peticion0.onreadystatechange = function() {
  52.                 if (peticion0.readyState == 4 && (peticion0.status == 200 || window.location.href.indexOf ("http") == - 1)){
  53.                     RESPUESTA0=peticion0.responseText;
  54.                         document.getElementById("resultado1").innerHTML =RESPUESTA0;                  
  55.                 }
  56.             if (peticion0.readyState == 4 && peticion0.status != 200){
  57.                 document.getElementById("resultado1")="FALLA EN LA COMUNICACION... SI EL PROBLEMA CONTINUA, FAVOR DE REPORTARLO";
  58.             }
  59.             }
  60.         }
  61.  
  62.         if(divrespuesta == 'resultado2'){
  63.  
  64.             peticion1=nuevoAjax();
  65.             peticion1.open("POST", "comparativas_info2.php", true);
  66.             peticion1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  67.             peticion1.send(cadenaFormulario);
  68.             peticion1.onreadystatechange = function() {
  69.                 if (peticion1.readyState == 4 && (peticion1.status == 200 || window.location.href.indexOf ("http") == - 1)){
  70.                     RESPUESTA1=peticion1.responseText;
  71.                         document.getElementById("resultado22").innerHTML =RESPUESTA1;                  
  72.                 }
  73.             if (peticion1.readyState == 4 && peticion1.status != 200){
  74.                 document.getElementById("resultado22")="FALLA EN LA COMUNICACION... SI EL PROBLEMA CONTINUA, FAVOR DE REPORTARLO";
  75.             }
  76.             }
  77.         }      
  78.     }

A ver si alguien sabe por que, un saludo.
  #2 (permalink)  
Antiguo 07/10/2015, 00:32
Avatar de Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: LLamando a una funcion dentro de javascritp no funciona

¿Haces las llamadas antes o después de que hayan cargado los elementos del documento?

Solo para descartar, intenta con esto:

Código Javascript:
Ver original
  1. document.addEventListener("DOMContentLoaded", function(){
  2.     enviarFormulario("comparativas_select.php", "formulario_1", "resultado");
  3.     enviarFormulario("comparativas_select.php", "formulario_2", "resultado2");
  4. }, false);

Un saludo
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand

Etiquetas: ajax, formulario, funcion, html, select
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 19:51.