Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.7 kB
1
Indexable
Never
 function formSubmitImport() {

        $("#form").submit(function(e) {
            e.preventDefault();
            $("#btn-submitImportPDP").prop("disabled",true);
            $("#btn-submitImportPDP").html("Loading...");
            var form = $('#form')[0];
            // Create an FormData object 
            var data = new FormData(form);

            $.ajax({
                url: '<?= Url::base() . "/promo-pdp/import"; ?>',
                type: "POST",
                enctype: 'multipart/form-data',
                processData: false, // Important!
                contentType: false,
                dataType: 'JSON',
                cache: false,
                data: data,
                beforeSend: function() {
                    $("#loading").show();
                    $("#submit").hide();
                    $("#bt_error").hide();
                    // document.getElementById("submit").disabled = true;
                },
                success: function(response) {
                    $("#btn-submitImportPDP").prop("disabled",false);
            $("#btn-submitImportPDP").html("Upload");
                    var data = response.data;
                    if (response.filesize > 5000) {
                        alert("Ukuran File maksimal 5 Mb");
                        $("#bt_error").show();
                    } else if (response.cek_count == false) {
                        alert("Maksimal 300 Data!");
                        $("#bt_error").hide();
                    } else if (response.total_error > 0) {
                        alert("Oops!! ada data yang error.");
                        $("#bt_error").show();
                    } else {
                        alert("Import Data Sukses!");
                        $("#bt_error").hide();
                    }
                    $("#loading").hide();
                    $("#submit").show();

                    $("#total_data").html("Total Data : " + response.total_data);
                    $("#total_sukses").html("Data Sukses : " + response.total_sukses);
                    $("#total_error").html("Data Error : " + response.total_error);
                    $("#file").val("");
                },
                error: function(xmlhttprequest, textstatus, message) {
                    if (textstatus === "timeout") {
                        alert("Proses timeout!");
                    } else {
                        alert(textstatus);
                    }
                    console.log("textstatus", textstatus);
                    console.log("message", message);
                    $("#loading").hide();
                    $("#submit").show();
                }
            });
        });
    }
Leave a Comment