Ciao a tutti mi sto cimentando ora con un pò di ajax, ora ho un problema:
fare un semplice upload con ajax, vi spiego come lo ho impostato,
ho la index.php così fatta:
ora vorrei sapere come far passare alla pagina upload il file come fosse un normale input di tipo file.Codice PHP:
<html>
<head>
<title>AjaxUpload</title>
<style type="text/css">
#form{
position: relative;
float: left;
width: 400px;
height: 200px;
background-color: blue;
color: yellow;
margin-top: 10px;
margin-left: 10px;
padding-top: 10px;
padding-left: 10px;
}
#resto{
position: relative;
float: left;
width: 400px;
height: 200px;
background-color: yellow;
color: black;
margin-top: 10px;
margin-left: 10px;
padding-top: 10px;
padding-left: 10px;
}
</style>
<script type="text/javascript">
var myRequest = null;
function CreateXmlHttpReq(handler) {
var xmlhttp = null;
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
xmlhttp.onreadystatechange = handler;
return xmlhttp;
}
function myHandler() {
if (myRequest.readyState == 4 && myRequest.status == 200) {
var e = document.getElementById("resto");
document.getElementById('sub').value = "Carica";
e.innerHTML =myRequest.responseText;
}
if (myRequest.readyState == 1) {
var e = document.getElementById("resto");
document.getElementById('sub').value = "Attendi in caricamento";
e.innerHTML ="attendi";
}
}
function upload() {
document.getElementById('sub').disabled = true;
myRequest = CreateXmlHttpReq(myHandler);
myRequest.open("POST","upload.php?rand="+escape(Math.random()));
myRequest.send(null);
}
</script>
</head>
<body>
<div id="form">
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="image"/>
<input id="sub" name="sub" type="button" onclick="upload();" value="Carica">
</form>
</div>
<div id="resto">
</div>
</body>
</html>
Grazie a chi mi spiega il meccanismo e Buon Anno Nuovo