php - Combine $_FILES and $_POST into one AJAX call with Javascript only -


i having difficulty sending ajax call contains both file , string. have no difficulty making either post or file, not both together. need in pure javascript, more proficient jquery.
here code working with...

    function uploadfile(){ var title = _('title').value; var genere = _('genere').value; var stars = _('stars').value; var description = _('description').value;     var file = _("video").files[0];      //alert(file.name+" | "+file.size+" | "+file.type);     var formdata =  new formdata(); formdata.append("video", file); formdata.append("title", title); formdata.append("genere", genere); formdata.append("stars", stars); formdata.append("description", description);     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", "video_php/video_upload.php");     ajax.send(formdata); } function progresshandler(event){     var percent = (event.loaded / event.total) * 100;     _("progressbar").value = math.round(percent);     _("status").innerhtml = math.round(percent)+"% uploaded... please wait"; } function completehandler(event){     _("status").innerhtml = event.target.responsetext;     _("progressbar").value = 0; } function errorhandler(event){     _("status").innerhtml = "upload failed"; } function aborthandler(event){     _("status").innerhtml = "upload aborted"; } 

this php.....

<?php $filename = ''; $filetmploc = ''; $filetype = ''; $filesize = ''; $title = ''; $genere = ''; $stars = ''; $description = ''; $retn=  ''; if (!isset($_files["video"]["name"])) { // if file not chosen     echo "error: please browse file before clicking upload button.";     exit(); }else{     $filename       =   $_files["video"]["name"]; // file name $filetmploc     =   $_files["video"]["tmp_name"]; // file in php tmp folder $filetype       =   explode('.',$filename); // type of file $filetype       =   end($filetype); // end type of file $filesize       =   $_files["video"]["size"]; // file size in bytes $title = preg_replace('#[^a-z0-9, ]#i', '', $_post['title']); $genere = preg_replace('#[^a-z0-9, ]#i', '', $_post['genere']); $stars = preg_replace('#[^a-z0-9, ]#i', '', $_post['stars']); $description = preg_replace('#[^a-z0-9, ]#i', '', $_post['description']); } echo $filename.'<br/>'; echo $filetmploc.'<br/>'; echo $filetype.'<br/>'; echo $filesize.'<br/>'; echo $title.'<br/>'; echo $genere.'<br/>'; echo $description.'<br/>'; ?> 

i have been stuck on weeks. have been through many other sites google. every answer close desired result has been in jquery. 1 solution had make 2-step form, uploading first 1 dataset, other, think both can sent user usability. have seen done on several other websites.
note*
above code has been corrected. added

formdata.append("video", file); formdata.append("title", title); formdata.append("genere", genere); formdata.append("stars", stars); formdata.append("description", description); 

it functional, @ohgodwhy

origional question @ send $_post , $_file data php javascript

looks want value of _('title') added formdata.

given have this:

formdata.append("video", file); 

all need this:

formdata.append('title', title); 

the formdata object handle transmission of file, , title available $_post['title'];


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -