Ver Mensaje Individual
  #24 (permalink)  
Antiguo 28/08/2013, 17:58
Erick_MD9
 
Fecha de Ingreso: julio-2013
Ubicación: México
Mensajes: 361
Antigüedad: 10 años, 9 meses
Puntos: 55
Respuesta: pasar variable con ajax y php

HOLA
ACA TE DEJO UNAS FUNCIONES SIN JQUERY


Código Javascript:
Ver original
  1. funciones js
  2. function nuevoAjax(){
  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. function ENVIAR(url, formid, divrespuesta){
  13.         var Formulario = document.getElementById(formid);
  14.         var longitudFormulario = Formulario.elements.length;
  15.         var cadenaFormulario="";
  16.         for (var i=0; i <= Formulario.elements.length-1;i++) {
  17.         if(Formulario.elements[i].type=="checkbox" || Formulario.elements[i].type=="radio"){
  18.             if(Formulario.elements[i].checked==true){cadenaFormulario += "&"+Formulario.elements[i].name+'='+encodeURIComponent(Formulario.elements[i].value);}
  19.         }else{cadenaFormulario += "&"+Formulario.elements[i].name+'='+encodeURIComponent(Formulario.elements[i].value);}
  20.         }
  21.     peticion=nuevoAjax();
  22.     peticion.open("POST", url, true);
  23.     peticion.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  24.     peticion.send(cadenaFormulario);
  25.     peticion.onreadystatechange = function() {
  26.         if (peticion.readyState == 4 && (peticion.status == 200 || window.location.href.indexOf ("http") == - 1)){
  27.             RESPUESTA=peticion.responseText;
  28.                 document.getElementById(divrespuesta).innerHTML =RESPUESTA;
  29.         }
  30.     if (peticion.readyState == 4 && peticion.status != 200){
  31.         document.getElementById(divrespuesta)="FALLA EN LA COMUNICACION... SI EL PROBLEMA CONTINUA, FAVOR DE REPORTARLO";
  32.     }
  33.     }
  34. }
Código HTML:
Ver original
  1. <div id="DIV_MUESTRA"></div>
  2. <form id="ID_FORM" action="#">
  3. <input type="text" name="ALGO" >
  4. <input type="text" name="ALGO2" >
  5. <input type="text" name="ALGO3" >
  6. <input type="buton" value="enviar" onclick=ENVIAR('tu_url','ID_FORM','DIV_MUESTRA'); >
  7. </form>
  8. </html>

Código PHP:
//EN PHP
$VAR1=$_POST['ALGO'];
$VAR1=$_POST['ALGO2'];
$VAR1=$_POST['ALGO3'];

ECHO
"BIEN"
Espero te sirva, Saludos.

Última edición por Erick_MD9; 28/08/2013 a las 18:15