I've been going mad this past month trying to build a page to submit a pdf asynchronously to our server. I'm an experienced programmer, but never worked in web-dev or in php or javascript.
The javascript submission function is as follows:
function sendFile(event)
{
$("#submitForm").hide();
var formData = new FormData();
var file=$("#journal")[0].files[0];
formData.append("document", file);
$.ajax("converter.php", { data:formData, processData:false, dataType:"text", method:"post", success:function(data, textStatus, jqXHR){
if(data.length == 8)
{
$("#key_input").val(data);
$("#key_submission").submit();
}
else
{
$("#response").html(data);
}
} });
It submits to the server but as soon the php script reaches this line
if(isset($_POST['document']))
the program crashes and I get an internal server error. What could be causing the crash?
This should be done with $.ajax()
this way:
$.ajax({ //<------"{" an object should be passed here so you are missing a "{"
url:"converter.php", //<-----"url:" was missing.
data:formData,
contentType:false, //<-----this is also needed for file upload
processData:false,
dataType:"text",
type:"post", //<------should be type not method
success:function(data, textStatus, jqXHR){
if(data.length == 8){
$("#key_input").val(data);
$("#key_submission").submit();
}else{
$("#response").html(data);
}
}
});
If you are trying to upload a file then contentType:false
is needed with processData:false
.