3
Good afternoon, I am using the following code to upload images:
function uploadImage($form){
$form.find('.progress-bar')
.removeClass('progress-bar-success')
.removeClass('progress-bar-danger');
var formdata = new FormData($form[0]); //formelement
var request = new XMLHttpRequest();
//progress event...
request.upload.addEventListener('progress',function(e){
$('.progress').removeClass('hidden');
var percent = Math.round(e.loaded/e.total * 100);
$form.find('.progress-bar').width(percent+'%').html(percent+'%');
});
//progress completed load event
request.addEventListener('load',function(e){
$('.progress').removeClass('active');
$form.find('.progress-bar').addClass('progress-bar-success').html('Upload completo');
});
request.open('post', 'server.php');
request.send(formdata);
}
the file server.php
contains:
<?php
foreach ($_FILES as $name => $file) {
$tmp_file = $file['tmp_name'];
$filename = $file['name'];
move_uploaded_file($tmp_file, 'uploads_folder/'. $name.$_POST['categoria_nome'].'.jpg');
}
return true;
? >
I wonder how I get the return
of the archive server.php
in the upload function?
In addition to the files I am sending another form fields, I will insert in the database within the file server.php, I would like to return something other than the server response. Can do ?
– Matheus Ilário
@Matheusilary what do you mean
retornar algo sem ser a resposta do servidor
? you can explain better?– Sergio
If the insertion is successful I want to return a variable with true value and another with a message for example
– Matheus Ilário
@Matheusilário ok, you can pass a JSON with
json.encode()
in PHP andJSON.parse()
no JS. If you do not know how to do I can put here an example later.– Sergio
would be grateful if you could post an example
– Matheus Ilário
@Matheusilary I put together at the end of the answer. That’s what you wanted right?
– Sergio
Okay Sergio, thank you very much! On Monday I take the test! Vlw
– Matheus Ilário
Sergio was just that! Thank you very much.
– Matheus Ilário
@Matheusilary great! See you soon.
– Sergio