Send text together with Formdata Ajax file

Asked

Viewed 135 times

0

I’m doing a test with the script below, for file upload:

function uploadFile(){
    var file = _("file1").files[0];
    // alert(file.name+" | "+file.size+" | "+file.type);
    var formdata = new FormData();
    formdata.append("file1", 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", "file_upload_parser.php");
    ajax.send(formdata);
}

What happens is that besides the file itself, I need to send 3 more text fields. I tried with a field as follows:

var teste = $('input:text[name=teste]').val();
formdata.append("teste", teste);

But the following error occurs:

inserir a descrição da imagem aqui

How can I send these variables? And arquivo file_upload_parser.php how can I receive these variables?

  • Diego it seems that the mistake is $ that is not set? you use some plugin or extension that needs the jQuery guy: _("file").files[0] ?

  • I’m not using plugins...

  • _("file1").files[0]; and that code comes from where?

  • var teste = $('input:text[name=teste]').val(); look where the problem is this is jQuery and you said you’re not using plugin or framework js may be the problem. Then use document.getElementById and take the value!

  • It is the _files form that is working. I will test with getElement

  • It worked with getElement. But how do I get it on the other page?

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.