zcriptz Seguir
1
Ómicron
Programador
Verificación en dos pasos desactivada
Verificado por Whatsapp
Suscripción a IA
- Desde
- 26 Mar 2013
- Mensajes
- 4.622
PHP:
<?php
if($_POST){ ?>
<b>Campo 1:</b> <?=$_POST['campo1']?><br>
<b>Campo 2:</b> <?=$_POST['campo2']?>
<? exit;
}
?>
<script>
function AJAX(u, c, d){
var XMLReq = new XMLHttpRequest();
XMLReq.onreadystatechange = function(){
if(XMLReq.readyState==4) c(XMLReq.responseText);
};
if(d) XMLReq.open('POST', u), XMLReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
else XMLReq.open('GET', u);
XMLReq.send(d)
}
function sendForm(f, c){
inputElements = f.querySelectorAll('input,textarea,select,button');
for(i = 0, postVar = ''; i < inputElements.length; postVar += inputElements[i].name+'='+inputElements[i].value+'&', i++);
AJAX(f.action, c, postVar);
return false
}
function byId(i){
return document.getElementById(i)
}
</script>
<form method="post" onsubmit="return sendForm(this, function(h){ byId('status').innerHTML = h })">
<b>Campo 1:</b><input type="text" name="campo1"><br>
<b>Campo 2:</b><textarea name="campo2" style="width:300px;height:100px"></textarea>
<input type="submit" value="Enviar">
</form>
<div id="status"></div>
Pueden definir a donde va a enviar AJAX colocandole "action" al FORM.
No hay mucho que explicar, ya es para usuarios más "avanzados".
Hay que ponerle al formulario
HTML:
onsubmit="return sendForm(this, CALLBACK)"
(CALLBACK, es en este caso es a donde "escribirá" la respuesta recibida, en el caso del demo, en el DIV con la ID "status")
Modificado: me faltaba un parámetro en la función ajax.
Última edición: