Trying to use a simple to upload images to back-end server using javascript and PHP only (No AJAX). I have decided to try appending the .files[0] data from input to the FormData in order to send it with the POST.
I would prefer not to use a submit form and would much rather use an httpRequest in order to get the pass/fail message as other things are to be done on the same page after the image/file is uploaded.
If possible, could someone please indicate to me a way of sending the data object through the httpRequest parameters. Once it reached the back-end, I can manipulate it fine, but I cannot find a way to send it that works via non AJAX methods.
HTML:
File Location: <br>
<input type='File' name='addNewMediaFileLocation' id='addNewMediaFileLocation' class='addFileText'><br>
Media Type: <select id='addFileFromLocalTypeSelect' class='addFileSelect>
<option>....</option></select>
<input type='button' class='profileButton' id='addMediaFromLocalButton' value='Upload' onclick="uploadMediaFromLocal('$ComicName', '$alias');"
JAVASCRIPT EDIT - Added getxml()
function getxml()
{
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function uploadMediaFromLocal(ComicName, UploadedBy)
{
var Type = document.getElementById("addFileFromLocalTypeSelect").value;
var file = document.getElementById('addNewMediaFileLocation').files[0];
var fd = new FormData();
fd.append("uploadedFile", file);
xmlhttp = getxml();
xmlhttp.open("POST", "php/actions.php?F=addMediaFromLocal" + "&Type="+Type, true);
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == XMLHttpRequest.DONE)
var response = xmlhttp.responseText;
if(response == 'Success')
{
document.getElementById("uploadMSG").innerHTML='Image upload successful';
document.getElementById("addNewMediaFileLocation").value="";
document.getElementById("addMediaDescriptionForLocalText").value="";
}
else
{
document.getElementById("uploadMSG").innerHTML=response;
}
}
xmlhttp.send(fd);
}
PHP
if($function == 'addMediaFromLocal')
{
$AddSuccess = true;
$AddError ='File not uploaded.';
$MediaType = $_REQUEST['Type'];
$FileType = $_FILES['uploadedFile']['type'];
$FileName = $_FILES['uploadedFile']['name'];
$FileContent = file_get_contents($_FILES['uploadedFile']['tmp_name']);
....VALIDATE INFO...
if($AddSuccess)
{
//check success
if(move_uploaded_file("../media/$name",$FileContent) )
{
print("Succes");
}
else
{
print($AddError. "Image upload unsuccessful, please check your folder permission<br>");
}
}
else
{
print($AddError);
}
}
ERROR:
E_NOTICE Error in file �actions.php� at line 1036: Undefined index: uploadedFile