ajax版本表单提交(包含图片上传)

26 篇文章 0 订阅

html:

<input type="file" id="uploadImg">

js:

var fd = new FormData();
      //fd.append("upload", 1);
      fd.append("file", $("#uploadImg").get(0).files[0]);
      console.log(fd);
      fd.append("storeName",storeName);
      fd.append("contactMobile",contactMobile);
      fd.append("address",address);
      fd.append("superName",superName);
      fd.append("superMobile",superMobile);
      fd.append("administrator",administrator);
      fd.append("administratorMobile",administratorMobile);
      fd.append("agent",agent);
      fd.append("pwd",pwd);

      $.ajax({
        type:'post',
        url:"http://localhost/COMM/uploadUser.php",
        processData: false,
        contentType: false,
        data: fd,
        dataType:'json',
        success:function(data){
          console.log("--------------");
          console.log(data);
          console.log(data.ok);
          if(data.ok==1){
              alert("Create succeed,your ID is:"+data.maxUserID+".Please keep it in mind!");
          }
        }
      })

php:

1、init.php

<?php  
    //init.php  
	header('Access-Control-Allow-Origin:*');
    $conn = mysqli_connect('127.0.0.1','root','','text',3306);  
    $sql = "SET NAMES UTF8";  
    mysqli_query($conn,$sql);  
?>  
2、uploadUser.php
<?php 

	require("init.php");
	
	$file = $_FILES['file'];
	$name = $file['name'];
	@$storeName = $_REQUEST["storeName"];
	@$contactMobile = $_REQUEST["contactMobile"];
	@$address = $_REQUEST["address"];
	@$superName = $_REQUEST["superName"];
	@$superMobile = $_REQUEST["superMobile"];
	@$administrator = $_REQUEST["administrator"];
	@$administratorMobile = $_REQUEST["administratorMobile"];
	@$pwd = $_REQUEST["pwd"];
	/*@$pwd = md5["pwd"];*/
	@$agent = $_REQUEST["agent"];
	
	$type = strtolower(substr($name,strrpos($name,'.')+1));
	$allow_type = array('jpg','jpeg','gif','png');
	
	if(!in_array($type, $allow_type)){
		return;
	}
	if(!is_uploaded_file($file['tmp_name'])){
		return;
	}
	
	$upload_path = "img/upload/";
	
	if(move_uploaded_file($file['tmp_name'],$upload_path.$file['name'])){
		
		
		$tmp_path=$upload_path.$name;
		
		$sql="INSERT INTO user(Agent,Password,StoreName,LogoPath,ContactMobile,Address,SuperviserName,SuperviserMobile,Administrator,AdministratorMobile) VALUES('$agent','$pwd','$storeName','$tmp_path','$contactMobile','$address','$superName','$superMobile','$administrator','$administratorMobile')";                                                                 
		
		$result=mysqli_query($conn,$sql);
		
		$count = mysqli_affected_rows($conn); 
		
		if($count>0){
			$sql="SELECT max(userID) FROM user";
			$result=mysqli_query($conn,$sql);
			$maxUserID=mysqli_fetch_row($result)[0];
			echo json_encode(['ok'=>1,'maxUserID'=>$maxUserID]);
		}else{
			echo json_encode(['ok'=>0]);
		}
		
	}else{
		echo json_encode(['ok'=>0]);
	}
	
	
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值