php - dynamically created HTML file input element doesn't post data -
i have form dynamically created using document.createelement. components created in same way. form used upload file php script , when submitted loads reponse in iframe specified target attribute of form.
unfortunately, server receives blank $_files array, , when inspect post request see file data wasn't included in request. if instead, on-the-fly, use google chrome developer tools edit dynamically created form (in process edit , change absolutely none of code), form submissions thereafter work perfectly, sending relevant file data.
so makes me realise there nothing wrong construction of form in html, because worked without changing anything. leads me believe browser doesn't me dynamically create file input elements?
for completeness' sake, here dynamically generated form:
<form method="post" action="includes/uploadpic.php" enctype="multipart/form-data" target="fileresponse"> <input type="file" name="pic"> <input type="submit" value="upload"> </form>
and print_r($_files)
gives empty array on server.
can explain me? alternative statically create hidden form in document, , append relevant div when need it, dislike kind of thing.
below code generates form:
var form = document.createelement("form"); var fileupload = document.createelement("input"); var uploadbut = document.createelement("input"); form.setattribute("method", "post"); form.setattribute("action", "includes/uploadpic.php"); form.setattribute("enctype", "multipart/form-data"); form.setattribute("target", "fileresponse"); fileupload.setattribute("type", "file"); fileupload.setattribute("name", "pic"); uploadbut.setattribute ("type", "submit"); uploadbut.setattribute ("value", "upload"); form.appendchild(fileupload); form.appendchild(uploadbut); dlgcontent.appendchild(form);
try this:
var form = document.createelement("form"); var fileupload = document.createelement("input"); var uploadbut = document.createelement("input"); form.method = "post"; form.action = "includes/uploadpic.php"; form.enctype = "multipart/form-data"; form.target = "fileresponse"; /* think trying submit in iframe */ fileupload.type = "file"; fileupload.name = "pic"; uploadbut.type = "submit"; uploadbut.value = "upload"; form.appendchild(fileupload); form.appendchild(uploadbut); dlgcontent.appendchild(form);
Comments
Post a Comment