Click here to Skip to main content
15,883,990 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to fire an ajax form submit when an html file input type changes and the file should be uploaded. but in the PHP file the filename is empty.

HTML

HTML
<form id="UploadForm" method="post" enctype="multipart/form-data">
<input type="file" id="upload" name="upload" >
</form>


Javascript:
JavaScript
unction uploadimg(){
		$.ajax({
			url:"uploadImg.php",
			data:{},
			success:function(response){
				console.log(response);
				if(response=="success"){
					alert("Image Uploaded Successfully");
				}
				else if(response=="Invalid"){
					alert("Invalid File Type");
				}
				else{
					alert("Image Not Uploaded");
				}
			}
		});
	}


PHP:
PHP
$allowedExts = array("gif", "jpeg", "jpg", "png");
	//$temp = explode(".", $_FILES["upload"]["name"]); //image
	//$extension = end($temp);
	echo $_FILES["upload"]["name"];
	$ext=pathinfo($_FILES["upload"]["name"],PATHINFO_EXTENSION);
	echo "--------------------".$ext."-------------------------";
	$src="";
	if ((($_FILES["upload"]["type"] == "image/gif")
	|| ($_FILES["upload"]["type"] == "image/jpeg")
	|| ($_FILES["upload"]["type"] == "image/jpg")
	|| ($_FILES["upload"]["type"] == "image/pjpeg")
	|| ($_FILES["upload"]["type"] == "image/x-png")
	|| ($_FILES["upload"]["type"] == "image/png"))
	&& in_array($extension, $allowedExts)) {
		$filename = $_SESSION['username']. $_FILES['upload']['name'];
		move_uploaded_file($_FILES['upload']['tmp_name'],"upload/". $filename);
		//Update Query
		
		$query="update js_basic_details set image='".$filename."' where email_id='".$_SESSION['username']."'";
		if(mysql_query($query))
			return "success";
		else
			return "Error";
	}
	else{
		echo "Invalid";
	}
Posted
Comments
Mohibur Rashid 20-Aug-15 21:35pm    
How is your uploadimg function is being called?
You are not sending any data with your uploadimg function.
read this link
This article explains how you can achieve uploading file. Please, read it carefully

1 solution

This is because you are not even trying to upload any files. To do that with a form, you don't even need any JavaScript. Just add a submit button and action to the form.
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900