2016-12-05 33 views
0

我知道這是一個簡單的問題,專家在這裏,但它一直困擾着我幾天。我是一名初學者,我認爲在處理數據方面存在一些問題。.ajax上傳文件併發送文本輸入到php,顯示錯誤信息但上傳成功

所以我這裏的目的是把用戶上傳的文件和電子郵件都發送到upload.php,然後upload.php會返回一個引用ID,然後顯示給用戶。

我面臨的問題是警告我與基準數的,相反,它會顯示兩個錯誤

  1. 未定義指數fileToUpload在XAMPP/htdocs中...
  2. 有一個在上傳文件時錯誤

但是,上傳文件是成功的,我可以看到在我的databas上傳的文件e和參考代碼已成功生成。

如果這兩個問題都解決了,我怎樣才能在HTML部分顯示參考代碼。謝謝!!!任何幫助感謝!

<form id="main-contact-form" class="main-contact-form" name="main-contact-form" method="post" enctype="multipart/form-data"> 
<div class="form-group"> 
    <input type="email" name="email" class="form-control" required="required" placeholder="Email Address"> 
    <input type="file" name="fileToUpload" id="fileToUpload" value="fileToUpload"> 
    <input type="submit" value="submit" name="submit" class="btn btn-primary pull-left crsa-selected"> 
</div> 
</form> 

這裏是我的阿賈克斯調用,將要發送的電子郵件地址,並上傳文件upload.php的

$(document).ready(function() { 
$("#main-contact-form").on('submit',(function(e) { 
e.preventDefault(); 
var formData = new FormData($(this)[0]); 
$.ajax({ 
    url: 'upload.php', 
    type: 'post',    
    data: formData, 
    cache: false, 
    contentType: false, 
    processData: false, 
    async: false, 
    success: function() 
    { 
    alert("ajax success"); 
    } 
}); 

function reqListener() { 
    console.log(this.responseText); 
} 

var oReq = new XMLHttpRequest(); 
oReq.onload = function() { 
    alert(this.responseText); 
}; 
oReq.open("get", "upload.php", true); 
oReq.send(); 
})); 
}); 

這是我upload.php的

<?php 
include("db.php"); 
$target_dir = ""; 
$target_file = ""; 

$target_dir = "submittedform/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$refId = ""; 
// upload file 
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) 
    { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded. <br/>"; 

     $refID = !empty($_POST['refID']) ? $_POST['refID'] : time() . rand(10*45, 100*98);; 

     // echo "Reference ID: " . $refID . "<br/>"; 
     echo json_encode("Reference ID: " . $refID . "<br/>"); 

     #once file uploaded, the path and reference code will be updated, status will be set to 1 
     $sqlInsert = "INSERT INTO student(reference_id, upload_appli_form, status) VALUES ('$refID', '$target_file', '1')"; 
     $qInsert = mysqli_query($db, $sqlInsert) or die("Error : ". mysqli_error($qInsert)); 

    } 

    else 
    { 
     echo json_encode("Sorry, there was an error uploading your file. <br/>"); 
    } 

    mysqli_close($db); 

?> 
+0

什麼是錯誤? –

+0

'async:false' is deprecated and will called error – ixpl0

+0

and why you do'var oReq = new XMLHttpRequest();'如果您已經使用了'.ajax();'? – ixpl0

回答

0

也許它必須是簡單?喜歡這個?

現在它是異步的。所以這將是有效的多年:D

$(document).ready(function() { 
    $("#main-contact-form").on('submit', (function(e) { 
     e.preventDefault(); 
     var formData = new FormData($(this)[0]); 
     $.ajax({ 
      url: 'upload.php', 
      type: 'post',    
      data: formData, 
      cache: false, 
      contentType: false, 
      processData: false     
     }).done(function(result) { 
      alert(result); //your POST answer 
     }); 
    })); 
});