echorand echorand - 2 months ago 10
Javascript Question

ajax success to call next function

I have this html which needs to upload the file to the server, then process it with another php and finally make the file available back on the html div, I am reading on ajax success calls which I can nest each function, but with being new to jquery and ajax function, I really don't know how to make this work...in the example below I can only call functions with buttons, and not ajax success calls...

<html>

<head>

</head>

<body>
<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">


function uploadFile(){
var file = document.getElementById("file").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "upload.php");
ajax.send(formdata);
}
function progressHandler(event){
document.getElementById("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
document.getElementById("progressBar").value = Math.round(percent);
document.getElementById("status").innerHTML = Math.round(percent)+"% uploaded... please wait";


}
function completeHandler(event){
document.getElementById("status").innerHTML = event.target.responseText;
document.getElementById("progressBar").value = 0;
}
function errorHandler(event){
document.getElementById("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
document.getElementById("status").innerHTML = "Upload Aborted";
}

function drive(){

var driver= new XMLHttpRequest();

driver.open("GET","drive.php",true);

driver.onreadystatechange= function(){
if(driver.readyState== 4 && driver.status == 200)
{

document.getElementById("response").innerHTML = driver.responseText;


}



}

driver.send();

}
function download(){

var download= new XMLHttpRequest();

download.open("GET","download.php",true);

download.onreadystatechange= function(){
if(download.readyState== 4 && download.status == 200)
{

document.getElementById("download").innerHTML = download.responseText;


}



}

download.send();

}








$.fn.YourName=function(){

uploadFile();
}

$.fn.drive=function(){

drive();

}



$(document).ready(function(){
$("#upload").click(function(){

uploadFile();

$.ajax({url: "upload.php", success: function(result){

$.ajax({url: "drive.php", success: function(result){
download();

}});

}});
});
});

</script>






<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="file" id="file"><br>
<input type="button" id="upload" value="upload File">
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>

<div id="response"></div>
<div id="download"></div>


</body>



</html>

Answer
$(document).ready(function(){
$("#upload").click(function(){

        uploadFile();

        $.ajax({url: "upload.php", success: function(result){

        $.ajax({url: "drive.php", success: function(result){
            download(); 

        }});

        }});
    });
});