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;
}
var xhttp = objetoAjax();
function enviar(form) {
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("respuesta").innerHTML = this.responseText;
} else {
document.getElementById("respuesta").innerHTML = "no enviado";
}
}
xhttp.open("POST","subir.php", true);
xhttp.send(new FormData(form));
}
-
puedes mostrar tu codigo html y php– Luciano Montañezel 24 jul. 2018 a las 22:37
-
si claro ahorita lo muestro– Vanegas9090el 25 jul. 2018 a las 4:26
-
pudiste probar la respuesta anterior ?– Luciano Montañezel 25 jul. 2018 a las 4:35
-
si pero igual me dio el mismo error, que no se recibe ningun archivo– Vanegas9090el 25 jul. 2018 a las 5:16
Añade un comentario
|
2 respuestas
Podrías resumir tu código con jquery, esto enviaría tu form por post, a la url indicada
$("#MyForm").submit(function(e) {
var url = "subir.php";
$.ajax({
type: "POST",
url: url,
data: $("#MyForm").serialize(),
success: function(data)
{
document.getElementById("respuesta").innerHTML = data;
},
error: function (xhr, ajaxOptions, thrownError) {
document.getElementById("respuesta").innerHTML = "no enviado";
}
});
e.preventDefault();
});
Este es el codigo HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>index</title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
<div id="respuesta">
hola mundo
</div>
<form id="form" action="javascript: enviar(this)">
<input type="file" name="archivo" id="archivo">
<input type="submit" value="enviar">
</form>
</body>
</html>
este es el codigo PHP
<?php
$nombre_temporal = $_FILES['archivo']['tmp_name'];
$nombre = $_FILES['archivo']['name'];
echo $nombre;
$destino = "archivos/" . $nombre;
move_uploaded_file($nombre_temporal, $destino);
?>