从PHP到jquery的返回值问题(使用ajax技术) [英] Problem with return value from PHP to jquery (with ajax technique)

查看:62
本文介绍了从PHP到jquery的返回值问题(使用ajax技术)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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>

推荐答案

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


.ajax({
url:admin.php,
类型:POST,
数据: new FormData(this),
contentType:false,
cache:false,
processData:false,
success:function(data){
var num = data。 search(invalid_file);
if(num!= - 1)
alert(图片大小不匹配......);
}
}) ;
});



PHP代码

if(
.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(
_SERVER['REQUEST_METHOD']=="POST" && isset(


这篇关于从PHP到jquery的返回值问题(使用ajax技术)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆