Upload csv file with ajax, json, php and mysql

0

I'm trying to make a import of a file csv to MySQL dealing with errors with JSon but I can not, even with a basic test I can not return the message, it always falls in else if .

This is the form:

<form method="post" enctype="multipart/form-data" id="frmImportacao">                       
    <input type="file" name="demo_file" class="file-upload-ajax">
</form> 

This is the script:

    $(document).ready(function(){       
    $('.file-upload-ajax').on('change',function(){              
        $(this).after('<span class="temp-message">Enviado...</span>');
        var formdata = new FormData($("#frmImportacao")[0]);
        $.ajax({
            type: "POST",
            url: "ajax/pUploadImportacaoRH.php",
            enctype: 'multipart/form-data',
            data: formdata,
            async: false,
            contentType: false,
            processData: false,
            cache: false,
            success: function(response) {
                if (response.codigo == "1") {
                $("#msgInsert").html('<div class="alert alert-success fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-times"></i><strong>AVISO!</strong>  ' + response.mensagem + '</div>');                        
                } else {
                    $("#msgInsert").html('<div class="alert alert-danger fade in"><button class="close" data-dismiss="alert">×</button><i class="fa-fw fa fa-times"></i><strong>ATENÇÃO!</strong>  ' + response.mensagem + '</div>');
                }
            }

        });
    });     
});

In php it looks like this:

$retorno = array('codigo' => 1, 'mensagem' => "RETORNO");
echo json_encode($retorno);
exit(); 

But it always gives me this error message:

    
asked by anonymous 06.02.2018 / 17:18

2 answers

0

The solution to my problem was solved as follows, following some tips here from the OS, I have re-formatted the form and the form of the submission.

        <form id="uploadForm" action="" method="post" class="form-horizontal">             
           <fieldset>
              <legend>Informe o arquivo</legend>
              <div class="form-group">
                 <label class="col-md-2 control-label">Escolher arquivo</label>
                 <div class="col-md-10">
                    <input name="userImage" type="file" class="btn btn-default" />                      
                 </div>
              </div>
           </fieldset>
           <div id="msgFoto" style="padding-top:10px;"></div>
           <div class="form-actions">
              <div class="row">
                 <div class="col-md-12">                        
                    <button class="btn btn-primary" type="submit"> <i class="fa fa-save"></i> Importar </button>
                 </div>
              </div>
           </div>
        </form>  
$(document).ready(function(e) {
        $("#uploadForm").on('submit', (function(e) {
            e.preventDefault();
            $.ajax({
                url: "ajax/pUploadImportacaoRH.php",
                type: "POST",
                data: new FormData(this),
                contentType: false,
                cache: false,
                processData: false,
                dataType: "json",
                success: function(data) {

                    if (data.codigo == "1") {
                        $("#msgInsert").html('×AVISO!' + data.mensagem + '');                          
                    } else {
                        $("#msgInsert").html('×ATENÇÃO! ' + data.mensagem + '');
                    }

                },
                error: function() {
                $("#msgInsert").html('×ATENÇÃO! Ocorreu um erro ao atualizar o arquivo. Contate o suporte técnico.');
                }
            });
        }));
    });
    
08.02.2018 / 10:53
1

The problem is that you are comparing a string with a number, so it will never be the same.

change this if (response.codigo == "1") { to this if (response.codigo == 1){

Since the return is a number and not a string:

$retorno = array('codigo' => 1, 'mensagem' => "RETORNO");

If an error appears in the header you need to set the return type, with this code:

header('Content-Type: application/json');

leaving your code like this:

$retorno = array('codigo' => 1, 'mensagem' => "RETORNO");
header('Content-Type: application/json');
echo json_encode($retorno);
exit(); 
    
06.02.2018 / 17:32