Save AJAX Data

Asked

Viewed 217 times

0

I need to send data via ajax to record, and one of the information is an array, but the array loses positions when I receive it in PHP, I don’t know if there is a size limit for the AJAX object sent, if you had to split the array into several as you would do multiple data uploads in the same ajax request?

var dadosPost = {
          'qtdNotas' : notasSelecionadasArray.length,
          'transacao': $("#transacao").val(),
          'flag_tipo_romaneio' : transacaoSelect.flag_tipo_entrega,
          'motorista': $("#motorista").val(),
          'veiculo':   $("#veiculo").val(),
          'placa':  veiculoSelect.placa,
          'ajudante1': $("#ajudante1").val(),
          'ajudante2': $("#ajudante2").val(),
          'notas': notasSelecionadasArray
      };



     if(validateState){
         $.ajax({
             //url da pagina
             url: $("#baseURL").val()+'romaneio/criarRomaneio',
             //url: $("#baseURL").val()+'/views/romaneio/tes.php',
             //parametros a passar
             data: (dadosPost),
             //tipo: POST ou GET
             //dataType: 'JSON',
             type: 'POST',
             //cache
             cache: false,
             beforeSend: function(){
                 $('.loading').css({display:"block"});

             },

The variable notes is an array with several selected notes, to avoid problems I am sending the amount of items contained in the array in JS to compare in PHP but this limits the application, I need to send a larger number of selected notes.

   PHP
    $notasSelecionadas = ($_POST['notas']);
    $qtdNotas = ($_POST['qtdNotas']);
    $transacao = $_POST['transacao'];
    $motorista = $_POST['motorista'];
    $veiculo = $_POST['veiculo'];
    $ajudante1 = $_POST['ajudante1'];
    $ajudante2 = $_POST['ajudante2'];
    $flagTipoRomaneio = $_POST['flag_tipo_romaneio'];


    if(count($notasSelecionadas) >= 1){

        if(count($notasSelecionadas) == $qtdNotas){

        $dets = array();

        foreach($notasSelecionadas as $n){

            $o_romaneioDet = new RomaneioDetModel();
             $o_romaneioDet->setNotaFiscal(DataFilter::numeric($n['no_docto']));
                $o_romaneioDet->setCodCliente(DataFilter::cleanString($n['cod_cliente']));
                $o_romaneioDet->setNomeCliente(DataFilter::cleanString($n['razao_cliente']));
                $o_romaneioDet->setEndCliente(DataFilter::cleanString($n['endereco']));
                $o_romaneioDet->setCidadeCliente(DataFilter::cleanString($n['cidade']));
                $o_romaneioDet->setBairroCliente(DataFilter::cleanString($n['bairro']));
                $o_romaneioDet->setFoneCliente(DataFilter::cleanString($n['fone']));
                $o_romaneioDet->setFaturamento($n['faturas']);
                $o_romaneioDet->setSerieNotaFiscal(DataFilter::cleanString($n['serie_nfe']));
                $o_romaneioDet->setModeloNf(DataFilter::cleanString($n['modelo_nf']));
                $o_romaneioDet->setDtEmissaoNf(DataFilter::cleanString($n['dt_emissao_nf']));

            array_push($dets, $o_romaneioDet);
  • tried to use like this: 'notas' : JSON.stringify(notasSelecionadasArray);

  • What you’re sending is object is not? array is not {} is [] dai if you’re going to access php by object would be dataPosts->qtdNotas example...

  • I’ll put as I do in PHP.

  • it is true what @Andersonhenrique said, if it is already converted into array does only call in php you use $notasArray = json_decode($_POST['notas']);

  • How you are sending the notesSelectionArray?

  • I have no problem receiving the values, I am receiving, the problem is that if I send 100 selected notes, in PHP it does not reach 100, depending on the amount of information contained in the notes array, the number of positions sent varies, for example, if I pick up 100 notes with Address Completed or is too much text, arrives only 40 note, if I take notes with no address with less information there arrives 60...

  • This is because the POST has size limit, what you could do is find in json and in php use the Code limit is 16.777.215 characters.

Show 2 more comments

1 answer

1


Pass the data via data: JSON.stringify(dadosPost), and in PHP capture the data, thus:

$dadosPost = json_decode($_POST['dadosPost']);
if (isset($dadosPost)) {
    // gravar dados no banco
}
  • That way it didn’t work, From the Internal error 500

  • Now it worked, after doing json_decode to access the data I have to use $n->no_docto, now I’m able to send 687 notes before only 44 thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.