RunNow
RunNow

Reputation: 11

$_POST["xml_submit"] is not set after request is posted, when using jquery.form

I am trying to make a file upload system with a progress bar. When I used the jquery.form plugin like I was told in the tutorials to do, the form's action was not executed. I thought I found a fix but now this seems to wipe the POST and FILES superglobals, perhaps because its not redirected directly by the form.

I am still a newbie to php, so I would appreciate help..

Here is the related code:

snippets of index.php:

<head>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.3.0/jquery.form.min.js" integrity="sha384-qlmct0AOBiA2VPZkMY3+2WqkHtIQ9lSdAsAn5RUJD/3vA5MKDgSGcdmIv4ycVxyn" crossorigin="anonymous"></script>
</head>
<script type="text/javascript" src="localCode/uploadProgress.js"></script>
            <div class="uploadxml_form"> 
                <form id="upload_form" action="?context=uploadxml" method="post" enctype="multipart/form-data">
                    Select file to upload:
                    <input type="file" name="xml_data_upload" id="xml_data_upload">
                    <input type="submit" value="Upload XML Data" name="xml_submit" onclick='uploadfile();'>
                </form>
            </div>

uploadProgress.js:

var progress_div
var form_div
var error_div

var form
var bar

$( document ).ready(function() { 
    progress_div = $('.uploadxml_progress');
    form_div = $('.uploadxml_form');
    error_div = $('.error');

    form = $('#upload_form');
    bar = $('#upload_progress');

    form_div.show();
    error_div.show()
    progress_div.hide();
    bar.val(0);
});

function uploadfile()  {
    form.ajaxForm({
        uploadProgress: function(event, position, total, percentComplete) {
            form_div.hide();
            error_div.hide()
            progress_div.show();
            bar.val(percentComplete);
        },
        complete: function(xhr) {
            window.location = form.attr("action")
        }
    });  
};

upload.php:

<?php
    if(isset($_POST["xml_submit"])) {
        $file = $_FILES["xml_data_upload"]["tmp_name"];
        if(!$file) {
            if(mime_content_type($file)=="text/html") {

            } else {
                header("Location: Projec/?context=uploadxml&error=filetype");
                exit();
            }
        } else {
            header("Location: Projec/?context=uploadxml&error=nofile");
            exit();
        }
    } else {
        header("Location: /Projec/index.php");
    }
?>

Thanks

Upvotes: 1

Views: 31

Answers (0)

Related Questions