php - Encountering an endless loop on an ajax function with no obvious loop -
i have file input declared follows:
<input id = "front_photo" type = "file" name = "front_photo" onchange = "send_photo()" /> the function calls declared follows:
function send_photo () { var fileinput = document.getelementbyid('front_photo'); var file = fileinput.files[0]; var formdata = new formdata(); formdata.append('front_photo', file); console.log("in"); var ajaxhandler = new xmlhttprequest(); ajaxhandler.onreadystatechange = function () { if(ajaxhandler.readystate == 4) { var picture_box = document.getelementbyid("polaroids_holder"); var adder = document.getelementbyid("add_polaroid"); var new_pic = document.createelement("div"); new_pic.classname = "polaroid"; new_pic.style.backgroundimage = "url('/assets/img/temp_front/"+ajaxhandler.responsetext+"')"; picture_box.insertbefore(new_pic, adder); send_photo(); } } ajaxhandler.open('post', 'upload_polaroid', true); ajaxhandler.send(formdata); } selecting file upload once causes go endless loops. "in" have there @ top piling in console log, i'm sure it's function being called repeatedly , not server side redirect. also, images being created on server. here's php that:
function upload_polaroid () { if(isset($_files['front_photo'])) { $format = explode(".", $_files["front_photo"]["name"]); $format = $format[1]; $filename = md5(mt_rand().microtime(true)).".".$format; $allowedtypes = array("image/tiff", "image/gif", "image/jpeg", "image/png", "image/x-bmp", "image/x-ms-bmp", ".bmp", ".dib", "image/vnd.microsoft.icon", "image/x-xbitmap", "image/x-xbm", ".xbm", ".cur"); if(in_array($_files["front_photo"]["type"], $allowedtypes)) { if(file_exists(getcwd()."/assets/img/temp_front/".$filename)) { chmod(getcwd()."/assets/img/temp_front/".$filename,0755); unlink(getcwd()."/assets/img/temp_front/".$filename); } move_uploaded_file($_files["front_photo"]["tmp_name"], getcwd()."/assets/img/temp_front/".$filename); $image_path = "/assets/img/temp_front/".$filename; echo $image_path; } else { echo "file not of valid image type."; die; } } else die; //var_dump($_files); } what going wrong here?
when ajax request successful code calls send_photo(); fires ajax request , calls send_photo(); method again when successful. causing code recursive.
function send_photo () { var fileinput = document.getelementbyid('front_photo'); var file = fileinput.files[0]; var formdata = new formdata(); formdata.append('front_photo', file); console.log("in"); var ajaxhandler = new xmlhttprequest(); ajaxhandler.onreadystatechange = function () { if(ajaxhandler.readystate == 4) { var picture_box = document.getelementbyid("polaroids_holder"); var adder = document.getelementbyid("add_polaroid"); var new_pic = document.createelement("div"); new_pic.classname = "polaroid"; new_pic.style.backgroundimage = "url('/assets/img/temp_front/"+ajaxhandler.responsetext+"')"; picture_box.insertbefore(new_pic, adder); /***********************************************/ send_photo(); //this making code recursive } } ajaxhandler.open('post', 'upload_polaroid', true); ajaxhandler.send(formdata); }
Comments
Post a Comment