At the moment I am working on some application, on which I do an AJAX-request which triggers a PHP script. This script echo's and returns some stuff while it is executing. Now would it be nice to be able to display this on the frontend, before .success() is triggered. Is this possible, and if so; how?
Cheers!
Updated with some code
Ajax + Submit (js)
$('document').ready(function() {
setAlbum.getForm.submit( function( e ) {
setAlbum.albumSubmit( e );
return false;
});
});
var setAlbum = {
getForm : $('#albumForm'),
createUrlList : function() {
var rawUrlList = setAlbum.getForm.find('#AlbumUrlList').val();
var urlList = rawUrlList.split(/\r?\n/g);
return urlList;
},
albumSet : function() {
var set = {
albumName : setAlbum.getForm.find('#AlbumName').val(),
albumDescription : setAlbum.getForm.find('#AlbumDescription').val(),
albumUrlList : setAlbum.createUrlList(),
};
return set;
},
albumSubmit : function( e ) {
e.preventDefault();
console.log( setAlbum.albumSet() );
$.ajax({
type: 'POST',
url: "query.php",
data: setAlbum.albumSet(),
success: function(data) {
console.log(data)
}
});
}
};
query.php (partial)
$naming = $_POST['albumName'];
$albumDescription = ['albumDescription'];
$photolist = $_POST['albumUrlList'];
$albumset = [
'AlbumName' => $naming,
'ItemList' => $photolist,
'AlbumDescription' => $albumDescription
];
/** GET FILESIZE CURL **/
function getFileSize( $url ) {
$headers = get_headers($url);
$fileSize = -1;
echo var_dump($headers);
echo $url;
foreach( $headers as $h ) {
preg_match( '/Content-Length: (\d+)/', $h, $m );
if ( isset($m[1]) ) {
$fileSize = (int)$m[1];
break;
}
}
$formattedFileSize = formatSizeUnits( $fileSize );
echo 'Filesize:: ' . $formattedFileSize;
return $fileSize;
}
//* FILESIZE FORMATTER *//
function formatSizeUnits( $bytes ) {
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
function getPhotosSize( $array, $maxSize ) {
$photos = $array['ItemList'];
$countTotal = sizeof($photos);
$count = 1;
foreach( $photos as $photo ) {
echo '<h2>Checking filesize…</h2>';
// saveImage( $img, $fullpath )
$url = $photo;
$photocount = sprintf('%02u', $count);
getFilesize($url);
}
}
getPhotosSize($albumset, $maxFileSize);