Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/09/2013, 09:20
Avatar de replica
replica
 
Fecha de Ingreso: noviembre-2004
Mensajes: 68
Antigüedad: 19 años, 5 meses
Puntos: 2
Duda (tonta) en la interacción ajax - php

Pongamos un archivo ajax.js típico (copiado de este foro, sin ir más lejos):

Código Javascript:
Ver original
  1. function objetoAjax(){
  2.     var xmlhttp=false;
  3.     try {
  4.         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  5.     } catch (e) {
  6.         try {
  7.            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  8.         } catch (E) {
  9.             xmlhttp = false;
  10.           }
  11.     }
  12.  
  13.     if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  14.         xmlhttp = new XMLHttpRequest();
  15.     }
  16.     return xmlhttp;
  17. }
  18.  
  19. function GuardarRegistro(){
  20.   //donde se mostrará lo resultados
  21.   divResultado = document.getElementById('resultado');
  22.   divResultado.innerHTML= '<img src="anim.gif">';
  23.   //valores de las cajas de texto
  24.   nombre=document.registro.nombre.value;
  25.   email=document.registro.departamento.value;
  26.   telefono=document.registro.telefono.value;
  27.   celular=document.registro.celular.value;
  28.  
  29. //instanciamos el objetoAjax
  30.  
  31.     ajax=objetoAjax();
  32.   //uso del medoto POST
  33.   //archivo que realizará la operacion
  34.   //registro.php
  35.   ajax.open("POST", "add.php",true);
  36.   ajax.onreadystatechange=function() {
  37.   if (ajax.readyState==4) {
  38.   //mostrar resultados en esta capa
  39.   divResultado.innerHTML = ajax.responseText
  40.   //llamar a funcion para limpiar los inputs
  41.   LimpiarCampos();
  42.   }
  43.   }
  44.  
  45.   ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  46.   //enviando los valores
  47.   ajax.send("nombre="+nombre+"&email="+email+"&telefono="+telefono+"&celular="+celular)
  48. }
  49.  
  50. function LimpiarCampos(){
  51.   document.registro.nombre.value="";
  52.   document.registro.departamento.value="";
  53.   document.registro.telefono.value="";
  54.   document.registro.celular.value="";
  55.  
  56.   document.registro.nombre.focus();
  57. }

Tengo una duda en la línea:

ajax.open("POST", "add.php",true);

¿Cómo puedo llamar directamente a una función incluida en ese archivo?
¿Le pongo un <?php require_once("add.php");?> arriba de todo, y en el open le pongo el nombre de la función?
¿O en el add.php le pongo un if isSet($_POST["enviar"])?