jQuery Ajax文件上传问题

时间:2018-09-30 03:49:28

标签: jquery ajax

我刚开始学习编程大约几个月。我尝试在不刷新页面的情况下进行文件上传。但文件未插入数据库。但是当我禁用ajax时,文件和其他信息将插入数据库。这是我的HTML。

<form action="" method="post" id="addTitle" enctype="multipart/form-data">

    <select class="form-control" name="albums" id="albums">
        <option value="select album">Select Album</option>
        <?php while ($album = mysqli_fetch_assoc($albums)) : ?> 
        <option value="<?= $album['album_id']; ?>"><?= $album['album_name']; ?></option>
        <?php endwhile; ?>
    </select><br>
    <select class="form-control" name="artist" id="artist">
        <option value="select artist">Select Artist</option>
        <?php foreach ($artists as $artist) :?>

        <option value="<?= $artist['artist_id']; ?>"><?= $artist['artist_name']; ?></option>
        <?php endforeach; ?>
    </select><br>
    <label for="addTitle">Add Title</label><br>
    <input class="form-control" type="text" name="addTitle" id="addTitleName" placeholder="Title"><br>

    <label for="audioFile">Audio File</label><br>
    <input class="form-control" type="file" name="audioFile"><br>

    <label for="addSrc">Download Link</label><br>
    <input class="form-control" type="text" name="addSrc" placeholder="Download Link"><br>

    <label for="audioDuration">Duration</label><br>
    <input class="form-control" type="text" placeholder="Audio Duration" name="audioDuration">

    <button class="btn btn-outline-secondary" name="inputFile">Add Title</button><span id="titleSuccess"></span>
</form><br><br>  

这是我的PHP。

function addTitle(){
global $conn;

$audioName = $_FILES["audioFile"]['name'];
$error = $_FILES["audioFile"]['error'];
$tmpName = $_FILES["audioFile"]['tmp_name'];
$ext = 'mp3';
$audioExt = explode('.', $audioName);
$audioExt = strtolower(end($audioExt));
$albumName = $_POST["albums"];
$titleName = $_POST["addTitle"];
$download = $_POST["addSrc"];
$duration = $_POST["audioDuration"];
$artist = $_POST["artist"];

if ($error === 4) {
    echo "Please Choose file";
    return false;
}

if (!$audioExt) {
    echo "Please Pick audio file";
    return false;
}

$checkTitle = mysqli_query($conn, "SELECT * FROM title WHERE title_name = '$titleName'");

$newAudioname = "MU_Seek-".uniqid();

move_uploaded_file($tmpName, 'audio/'.$newAudioname.".".$audioExt);

$audioFIleSize = $_FILES["audioFile"]["size"];
// $checkFile = mysqli_query($conn, "SELECT * FROM title WHERE audio_source = '$newAudioname'");
$checkDownload = mysqli_query($conn, "SELECT * FROM title WHERE download_link = '$download'");

if (mysqli_fetch_assoc($checkTitle)) {
    echo "Title already Exist";
    return false;
} 
// else if(mysqli_fetch_assoc($checkFile)){
//      echo "File already exsist";
//      return false;
// } 
else if(mysqli_fetch_assoc($checkDownload)){
    echo "Download link already exsist";
    return false;
}else{
    $insert = "INSERT INTO title (title_name, artist_id, album_id, audio_source, download_link, file_size,duration) VALUES ('$titleName', '$artist', '$albumName','$newAudioname', '$download', '$audioFIleSize','$duration')";
}
$query = mysqli_query($conn, $insert);
if (!$query) {
    echo "Failed to Add title";
    return false;
} else{
    header("Location: upload.php");
}

}

这是我的jquery。

$('#addTitle').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: 'upload.php',
        enctype: 'multipart/form-data',
        contentType: false,
        processData: false,
        cache: false,
        data: new FormData(this),
        success: function(data) {
            if ($.trim($('#addTitleName').val()) == "") {
                $('#titleSuccess').fadeIn(1000);
                $('#titleSuccess').html('Please input title name');
                $('#titleSuccess').css({
                    'color': 'red',
                    'font-style': 'italic'
                });
                $('#titleSuccess').fadeOut(3000);
                return false;
            } else {
                $('input').val('');
                $('#titleSuccess').fadeIn(1000);
                $('#titleSuccess').html('Title added successfully');
                $('#titleSuccess').css({
                    'color': 'lime',
                    'font-style': 'italic'
                });
                $('#titleSuccess').fadeOut(3000);

            }

        }
    })
})  

对不起,我的英语不好,因为英语不是我的主要语言。 希望你能理解我的问题。
和平。

0 个答案:

没有答案