hi, i am creating a form that could browse and retrive a single file. please see my codes.
//create the form
myForm=document.createElement("form");
myForm.action="uploader.php";
myForm.method="post";
myForm.enctype="multipart/form-data";
rightDiv.appendChild(myForm);
hidden1=document.createElement("input");
hidden1.type="hidden";
hidden1.name= "MAX_FILE_SIZE";
hidden1.value= 2000000;
myForm.appendChild(hidden1);
//for the browse button
bttn = document.createElement("input");
bttn.type="file";
bttn.name="upload";
bttn.value="";
bttn.style.position="absolute";
myForm.appendChild(bttn);
//for submit button
bttn1 = document.createElement("input");
bttn1.type="submit";
bttn1.value="Submit";
bttn1.style.position="absolute";
bttn1.onclick=passForm;
myForm.appendChild(bttn1);
hidden2=document.createElement("input");
hidden2.type="hidden";
hidden2.name="submitted";
hidden2.value="TRUE";
myForm.appendChild(hidden2)
once the button Submit is click it wil call the function passForm to hopefully send the input element's value to a PHP file.
function passForm() {
myForm.submit();
}
and here is my PHP codes (uploader.php) that will handle the data from javascript codes.
<?php
if (isset($_POST['submitted'])) {
// Check for an uploaded file.
if (isset($_FILES['upload'])) {
// Validate the type.
$allow_pic = array ('image/gif', 'image/jpeg', 'image/jpg', 'image/pjpeg');
$allow_doc = array ('application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/pdf');
if (in_array($_FILES['upload']['type'], $allow_doc) || in_array($_FILES['upload']['type'], $allow_pic)) {
if (move_uploaded_file($_FILES['upload']['tmp_name'], "../documents/{$_FILES['upload']['name']}")) {
echo '<p>The file has been uploaded!</p>';
} else { // Couldn't move the file over.
echo '<p><font color="red">The file could not be uploaded.</b>';
}
} else { // Invalid type.
echo '<p><font color="red">File format is not supported.</font></p>';
}
} else { // No file uploaded.
echo '<p><font color="red">Please upload a MS Word, MS PowerPoint, MS Excel, or PDF File smaller than 2MB.</font></p>';
$_FILES['upload']; // to check if this global variable is set(and/or has value).
}
} // End of the submitted conditional.
?>
unfortunately, i got this result.
Please upload a MS Word, MS PowerPoint, MS Excel, or PDF File smaller than 2MB.
Notice: Undefined index: upload in F:\Program Files\Apache Group\Apache2\htdocs\DCFA\test1\sections\members\uploader.php on line 70
one thing for sure the value, supposedly for $_FILE['upload'], didn't reach uploader.php. i guess my javascript codes just lack something,
or is there a better way around?