Click here to Skip to main content
15,031,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote code below using PHP, mySQL and AJAX to form submission.
When the user wants to upload an image larger than 700*400, the alert message such as "the image size doesn't match" doesn’t show up. Why?


What I have tried:

JQuery Code

$("#addproduct").submit(function(event){	
	event.preventDefault();

	$.ajax({
		url: "admin.php", 
		type: "POST",             
		data: new FormData(this), 
		contentType: false,       
		cache: false,             
		processData:false,        
		success: function(data){ 
				var num=data.search("invalid_file");
				if(num!=-1)
					alert("the image size doesn't match...");
		}
	});
});



PHP Code

if($_SERVER['REQUEST_METHOD']=="POST" && isset($_POST['tahahiddenadd'])){
	
	$pName=$_POST['tahaproductname'];
	$pPrice=(int)$_POST['tahaproductprice'];
	$pDesc=$_POST['tahaproductdesc'];
	$pPath="image/site/noProductPic.jpg";
	
	if(isset($_FILES['tahaproductpicpath'])){
		if($_FILES['tahaproductpicpath']['error']==0 && ($_FILES['tahaproductpicpath']['type']=="image/jpeg" || $_FILES['tahaproductpicpath']['type']=="image/gif" || $_FILES['tahaproductpicpath']['type']=="image/png")){
			$imageSize=getimagesize($_FILES['tahaproductpicpath']['tmp_name']);
			$width=$imageSize[0];
			$height=$imageSize[1];
			
			if($width>700 || $height>400){
				echo "invalid_file";
				exit;
			}
			else{
				move_uploaded_file($_FILES['tahaproductpicpath']['tmp_name'],'image/product/'.$_FILES['tahaproductpicpath']['name']);
				$pPath="image/product/".$_FILES['tahaproductpicpath']['name'];
			}
		}
	}

	$con=mysqli_connect("localhost","root","");
	if(!$con)
		die("");
	else{
		mysqli_select_db($con,"tahaDB");
		mysqli_query($con,"insert into tahaproducttable(productName,productPrice,productDesc,productPicPath) values('$pName',$pPrice,'$pDesc','$pPath')");
		mysqli_close($con);
		
	}
}



HTML From

<form id="addproduct" enctype="multipart/form-data" style="direction:rtl">
    <input type="text" name="tahaproductname" id="tahaproductname" maxlength="50" />
    <input type="text" name="tahaproductprice" id="tahaproductprice" maxlength="10" />
    <textarea name="tahaproductdesc" id="tahaproductdesc" maxlength="300"></textarea>
    <input type="file" name="tahaproductpicpath" id="tahaproductpicpath" accept="image/jpeg, image/gif, image/png"  /> 
    <input type="hidden" name="tahahiddenadd" id="tahahiddenadd" value="tahahiddenadd" />
    <input type="submit" name="tahasubmitadd" id="tahasubmitadd" value="Add Product" />
</form>
Posted
Comments
David_Wimbley 10-Jul-17 12:56pm
   
I don't see in your PHP where you are returning back anything so that your jquery can access it via the data object in the .success function call back.

Also, why are you using data.search(? If you return the object in your PHP (i believe you have to serialize it first in PHP), then you'd be able to access it in your jquery as simply data.your_property_to_access_and_use.
Pouria Polouk 10-Jul-17 16:44pm
   
Thank you, but I think we can return a value to JQuery using "echo" in PHP.
isn't it like this?

In fact my code doesn't work as following:
if(data=="invalid_file")
alert("the image size doesn't match...");
David_Wimbley 10-Jul-17 16:51pm
   
I was specifically looking for json_encode function. This is probably why you aren't getting any results as you aren't defining using header('Content-type: application/json'); nor are you using json_encode.

http://php.net/manual/en/function.json-encode.php
Pouria Polouk 10-Jul-17 17:12pm
   
I have used json_encode function before.
But it didn't work.
I don't know why!?

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