Tengo lo siguiente:
"funcionAjax.js"
Código:
"index.php"function objetoAjax()
{
var xmlhttp=false;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function pedirDatos(url,capa,valores,metodo)
{
var ajax=objetoAjax();
var capaContenedora = document.getElementById(capa);
ajax.open ('POST', url, true);
ajax.onreadystatechange = function()
{
if (ajax.readyState==4)
{
document.getElementById(capa).innerHTML=ajax.responseText;
}
}
ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
ajax.send(valores);
return;
}
Código HTML:
<html> <head> <title>Probando Ajax</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src="funcionAjax.js"></script> </head> <body> <form method="post" action="#"> <input type="text" id="campo" /><br> Opcion 1: <input type="radio" name="opcion" id="opcion" Value="opcion 1" /><br> Opcion 2: <input type="radio" name="opcion" id="opcion" Value="opcion 2" /><br> <input type="submit" value="enviar" onclick="pedirDatos('mostrar.php','capaContenedora', 'campo='+document.getElementById('campo').value+'&opcion='+document.getElementById('opcion').value,'POST'); return false" /> </form> <div id="capaContenedora"></div> </body> </html>
Código HTML:
<?php
echo "texto ingresado: ".$_POST["campo"]."<br>";
echo "opcion seleccionada: ".$_POST["opcion"];
?> Aclaro que soy novato en esto de ajax, y arme estos códigos de muchos ejemplos de internet y los adapté a mi forma de trabajar.


