dqb78642 2014-12-02 08:16
浏览 70

php mysql ajax注册表单图片上传

hello brothers i would like to ask you how can make a registration form with image upload(base64) usign php mysql ajax and this is a part of my code but it didn't work. i wish if you tell me the type of table row and give me the righ code to do this.

$("#dsubmit").click(function(){
    var formData = new FormData(this);
    demail=$("#demail").val();
    dpassword=$("#dpassword").val();
    dfirstname=$("#dfirstname").val();
    dlastname=$("#dlastname").val();
    dtel=$("#dtel").val();
    dadr=$("#dadr").val();
    dspeciality=$("#dspeciality").val();
    dcodepost=$("#dcodepost").val();

    $.ajax({
        type: "POST",
        url: "inc/regdoc.php",
        data: formData,"&demail="+demail+"&dpassword="+dpassword+"&dfirstname="+dfirstname+"&dlastname="+dlastname+"&dtel="+dtel+"&dadr="+dadr+"&dspeciality="+dspeciality+"&dcodepost="+dcodepost,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(html){
            if(html=='true') {
                $("#dmsg_box_connexion").css("color","green");
                $("#dmsg_box_connexion").html("Utilisateur Ajouté avec succés ! Redirection ...");
                window.setTimeout(function(){window.location.href = "index.php";}, 5000);
            } else { 
                $("#dmsg_box_connexion").html("S'il vous plaît remplir tous les champs");
            }
        },
        beforeSend:function() {
            if((demail == "")||(dfirstname == "")||(dlastname == "")||(dtel == "")||(dpassword == "")||(document.getElementById("dfile").value == "")||(dcodepost == "")||(dadr == "")) {
                $("#dmsg_box_connexion").css("color","red");
                $("#dmsg_box_connexion").html("Tous Les Champs Sont Obligatoires !");
                return false;
            }

            $("#dmsg_box_connexion").css("clor", "#32b1d3");
            $("#dmsg_box_connexion").html("Validation...");
        }
    });
    return false;
});

});

and this is the php file :

session_start();
$email = addslashes(htmlentities($_POST["demail"]));
$password = addslashes(htmlentities($_POST["dpassword"]));
$firstname = addslashes(htmlentities($_POST["dfirstname"]));
$lastname = addslashes(htmlentities($_POST["dlastname"]));
$codepost = addslashes(htmlentities($_POST["dcodepost"]));
$adresse = addslashes(htmlentities($_POST["dadr"]));
$tel = addslashes(htmlentities($_POST["dtel"]));
$speciality = addslashes(htmlentities($_POST["dspeciality"]));
$get_content = file_get_contents($_FILES['dfile']['tmp_name']);
$escape = mysql_real_escape_string($get_content);
$sourcePath = $_FILES['dfile']['tmp_name'];   // Storing source path of the file in a variable
$targetPath = "uploads/".$_FILES['dfile']['name'];  // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; //  Moving Uploaded file  
$pass = sha1($password);
include ('pgs/config.php');
$insert = $bdd->query("INSERT INTO tbl_docs VALUES      ('','$firstname','$lastname','$tel','$adresse','$speciality','$email','$pass','$escape','1','$codepost')");
if($insert == 1) {
    echo 'true';
} else {
    echo 'false';
}

and this is th form header:

<form id="d" method="post" action="#inc/regdoc.php" enctype="multipart/form-data">
  • 写回答

1条回答 默认 最新

  • dtdt0454 2019-07-06 04:44
    关注

    Check the working example i have implemented in my project how to submit a form with image and some data fields.

    $(document).on('submit', '#catcategory-form', function(event) {
          event.preventDefault();
          $.ajax({
              url: "product.php",
              method: 'POST',
              data: new FormData(this),
              dataType: 'json',
              contentType: false,
              processData: false,
              success: function(infodata) {
              if(infodata=='true'){
    
              //do the stuff whatever you want
    
              }
    
          });
      });
    

    The another option you can do with image upload is convert base64. What you have to do is convert you form image in base 64 and send it on onChange event to the php and upload it in a file and keep the unique name in your database

    //Call this function after getting base64 by post
    
    $imageBase64=$_POST['image_converted_base64'];//get base64 of image from client end
    
     $unique_name =uploadSingleImage($imageBase64);//function call
    
    //function to upload image and get an unique name to store in db
    
        function uploadSingleImage($base64) {
    
            $uniname = uniqid() . date("Y-m-d-H-i-s") . ".jpg";
            $new_image_url = "../../image/" . $uniname;
            $base64 = 'data:image/jpeg;base64,' . $base64;
            $base64 = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64));
            file_put_contents($new_image_url, $base64);
            return $uniname;
        }
    
    $sql = "INSERT INTO `table1`(image_name) VALUES('$unique_name')";
    $conn->query($sql);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection