Hi I am not able to get PHP FILES values in view.php
, whereas normally it is found in view.php
. Please help me out. Thanks in advance.
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<title>Untitled Document</title>
<script type="text/javascript">
$( document ).ready(function() {
$('#uploadBtn').click(function() {
$.ajax( {
type: 'POST',
contentType:attr( "enctype", "multipart/form-data" ),
url: 'view.php',
data: $('myform').serialize() ,
success: function(data){
alert(data);
}
});
});
});
</script>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="view.php" name="myform">
<input id="f" type="file" value="" name="myfile"/>
<input type="button" value="try" id="uploadBtn" />
</form>
</body>
</html>
The error you are getting is caused by trying to use "attr" as if it were a variable. You don't have to set the enctype
of an AJAX request because that refers to forms only. You could set the appropriate Content-type
though (to "multipart/form-data"
)
Aside from that, you are trying to upload a file using AJAX, which is not always possible. See this question for reference.
Quoting the accepted answer from that question:
With XHR2, File upload through AJAX is supported. E.g. through FormData object, but unfortunately it is not supported by all/old browsers.
I would recommend you try an AJAX file upload plugin, they usually do the job very well and will ensure cross-browser compatibility.