We help IT Professionals succeed at work.
Get Started

AJAX File Upload Form

dban00b asked
Last Modified: 2012-06-21
my below javascript code was working fine for submitting forms until I tried to add a file input to the form.

the php file, response_test.php, is saying that the $_FILES array is empty

It must having something to do with how I'm stitching together a parameter string:


response_test.php sees $_POST['description'], $_POST['user'] but does not see $_FILES['file']

How do I fix what I'm sending to the php page so that it can see the file I'm trying to upload through the form?
-----The HTML Form-------------------------------------------------------
<form enctype="multipart/form-data" action="javascript:submitform();" name="form" id="form" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="31457280" />
Enter a file Description <input name="description" type="text" id="description" /><br />
Choose a file to upload: <input name="file" id="file" type="file" /><br />
<input type="hidden" name="user" id="user" value="testuser" />
<input type="submit" value="Upload File" />
-----The Javascript-------------------------------------------------------
function submitform(){
	var params=verifynewincform();
		//placeholder image
		document.getElementById('mydiv').innerHTML='<img src="/images/loading.gif" />';	
		document.getElementById('mydiv').innerHTML ='You did not fill in the information correctly';
function verifynewincform(){
	var valid=true;
	var params='';
	//test form fields here
	if(!valid) {
		return false;
	else {
		return params;
function newRequest(parameters) {
	myXHR = false;
	if (window.XMLHttpRequest) {
		myXHR = new XMLHttpRequest();
		if (myXHR.overrideMimeType) {
	} else if (window.ActiveXObject) {
		try {
			myXHR = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				myXHR = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
	if (!myXHR ) {
		document.getElementById('mydiv').innerHTML='Cannot create XHR ';
		return false;
	myXHR.onreadystatechange = newResponse;
	myXHR.open('POST', 'response_test.php', true);
	myXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	myXHR.setRequestHeader("Content-length", parameters.length);
	myXHR.setRequestHeader("Connection", "close");
function newResponse() {
	if (myXHR.readyState == 4) {
		if (myXHR .status == 200) {
			result = myXHR .responseText;
            document.getElementById('mydiv').innerHTML = result;            
		} else {
            document.getElementById('mydiv').innerHTML ='myXHR error:'+myXHR .status;

Open in new window

Watch Question
PHP Programmer
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE