如何从注册表单发送图像数据和其他参数到PHP

时间:2017-08-24 09:44:54

标签: php ajax

这是我的ajax代码

var fd = new FormData();    
            fd.append( 'file', $('#img')[0].files[0]);

var data = '&com='+company+'&loc='+location+'&year='+year+'&desc='+des+'&userid='+userid+'&fd='+fd;



        $.ajax({
             type : "POST",
             url: "insert.php",
             data : data,


                 success: function (response){



                 }
          });

这是php代码

$conn =new mysqli("localhost", "root", "","test2");


 $company=isset($_POST['com']) ? $_POST['com']: '';

$local=isset($_POST['loc']) ? $_POST['loc']: '';
$year=isset($_POST['year']) ? $_POST['year']: '';
$description=isset($_POST['desc']) ? $_POST['desc']: '';
$userid=isset($_POST['userid']) ? $_POST['userid']: '';


$query = mysqli_query($conn,"call exp('$userid', '$company', '$local', '$year', '$description')");

$target = "C:/xampp/htdocs/img/";
$target = $target . basename( $_FILES['file']['name']);

$Filename=basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
    $conn =new mysqli("localhost", "root", "","test2");

   $query = mysqli_query($conn,"INSERT INTO experience (image , PersonID) VALUES ('$Filename','$userid')");
    print_r($query);


} else {

    echo "Sorry, there was a problem uploading your file.";
}

如何在ajax中发送param和image ....因为我只能分别发送图像和数据中的数据。因为我尝试了许多不同的代码,但没有完美地运作

1 个答案:

答案 0 :(得分:0)

var guestbookSendMessage = new FormData();

guestbookSendMessage.append('com',company);
guestbookSendMessage.append('loc', location);
guestbookSendMessage.append('year',year);
guestbookSendMessage.append('desc',des);
guestbookSendMessage.append('userid',userid);

guestbookSendMessage.append('file', $("#img")[0].files[0]);

        $.ajax({
             type : "POST",
             url: "insert.php",

             data : guestbookSendMessage,
             cache: false,
               contentType: false,
               processData: false,


                 success: function (response){



                 }
          });

这是怎么做的.....