简体   繁体   中英

How to avoid move_uploaded_file() refresh the page on button click

I am using $.ajax({}) of jQuery, I am trying to move the uploaded file in folder on click. The upload came successful but my problem is when uploading, the page is refreshing I already tried e.preventDefault() and <button type="button">

This is my html code

<input type="file" id="student-img-file" accept="image/*" name="student-img-file">
<button type="button" id="btn-submit-form" class="mt-4">Submit Form</button>

This is my jQuery code

$('button#btn-submit-form').on('click', function(e){
    let formData = new FormData()
    formData.append('student-img-data', $('#student-img-file').prop('files')[0])
    
    $.ajax({
        url: '../PHPFunctions/AdmissionFormFunction.php',
        method: 'POST',
        data: formData,
        contentType: false,
        processData: false,
        cache: false,
        success: function(result){
            console.log(result)
        }
    })
})

this is my PHP(AdmissionFormFunction.php) code

$studentImgData = $_FILES['student-img-data'];

if(move_uploaded_file($studentImgData['tmp_name'], '../FileUploads/' . $studentImgData['name'])){
    echo "YESSSSSSSSSSSS";
}

I am not using form tags by the way

I just turned off my vs code live server. I forgot that any changes that i've made in my folder system in vs code will refresh the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM