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]);
}
?>