0
這是我的Ajax代碼如何從登記表發送圖像數據和其它參數去PHP的
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和圖像...因爲我只能分別發送圖像和數據分開的數據。因爲我已經嘗試了很多不同的代碼,但不完全
你需要設置的內容適當的AJAX選項類型。 – ADyson
如何設置 – knight007
如果你谷歌它已被問很多次。這裏只是一個例子https://stackoverflow.com/a/5976031/5947043 – ADyson