.ajax用于上传文件并将文本输入发送到php,显示错误消息但上传成功

时间:2016-12-05 07:01:26

标签: javascript php jquery ajax file-upload

我知道这对于这里的专家来说是一个简单的问题,但它已经困扰了我几天。我是初学者,我认为处理数据存在一些问题。

所以我的目的是获取上传的文件和用户输入的电子邮件,将其发送到upload.php,然后upload.php将返回一个引用ID,然后将其显示给用户。

我遇到的问题不是提醒参考号码,而是显示两个错误

  
      
  1. xampp / htdocs中的未定义索引fileToUpload ...
  2.   
  3. 上传文件时出错
  4.   

但是,上传文件成功,我可以看到我的数据库中的上传文件和参考代码生成成功。

如果解决了这两个问题,我如何在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>

这是我的.ajax调用,用于将电子邮件地址和上传的文件发送到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);

?>

1 个答案:

答案 0 :(得分: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
        });
    }));
});