带图像上传的php mysql ajax注册表单

带图像上传的php mysql ajax注册表单,php,mysql,ajax,forms,post,Php,Mysql,Ajax,Forms,Post,您好,兄弟们,我想问您如何使用php mysql ajax制作带有图像上传(base64)的注册表单,这是我代码的一部分,但它不起作用。我希望您能告诉我表格行的类型,并给我执行此操作的正确代码 $("#dsubmit").click(function(){ var formData = new FormData(this); demail=$("#demail").val(); dpassword=$("#dpassword").val(); dfirstname

您好,兄弟们,我想问您如何使用php mysql ajax制作带有图像上传(base64)的注册表单,这是我代码的一部分,但它不起作用。我希望您能告诉我表格行的类型,并给我执行此操作的正确代码

$("#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;
});
}))

这是php文件:

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';
}
这是th表格标题:

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

检查我在项目中实现的如何提交带有图像和一些数据字段的表单的工作示例

$(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

          }

      });
  });
图像上传的另一个选项是convert base64。您需要做的是在base64中转换您的表单图像,并在onChange事件中将其发送到php,然后将其上传到文件中,并在数据库中保留唯一的名称

//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);

它不起作用。它会出错吗?在格式化代码时,我注意到javascript在
beforeSend
函数中缺少一个else,而您有一个});对许多人来说。。我不知道您使用的编辑器是什么,但如果您正确格式化代码,您自己就会注意到。您没有在数据库中保存任何与图像相关的内容。在这里,您可以找到一个示例以及不同解决方案的优缺点: