Get data fields array () jQuery

1

I have the following fields in array ():

<form enctype="multipart/form-data" id="enviar" method="post">
    <table class="table" style="border-top: 1px solid #FFF !important;">
        <tr>
            <td>L</td>
            <td>Q</td>
            <td>Inicio</td>
            <td>Fim</td>
        </tr>
        <?php 
            for($i=1; $i<=$_GET['lotes']; $i++){ 

                if($i==$_GET['lotes']){
                    $dados['monta_lote'] = $dados['monta_lote'] + $dados['calcula_sobra'];
                }
        ?>  
        <tr>
            <td><span class="widget-thumb-body-stat label label-danger btn-circle" data-counter="counterup" data-value="1"><?php echo $i; ?></span></td>
            <td><span class="widget-thumb-body-stat label label-danger btn-circle" data-counter="counterup" data-value="<?php echo $dados['monta_lote']; ?>"><?php echo $dados['monta_lote']; ?></span></td>
            <td>
                <input type="hidden" id="mensagem" name="mensagem" value="<?php echo $_GET['mensagem']; ?>">
                <input type="hidden" id="lote[]" name="lote[]" value="<?php echo $i; ?>">
                <input type="hidden" id="quantidade[]" name="quantidade[]" value="<?php echo $dados['monta_lote']; ?>">
                <input style="width:145px;" type="date" id="data_inicio[]" name="data_inicio[]" required>
                <input style="width:60px;" type="time" id="hora_inicio[]" name="hora_inicio[]" required>
            </td>
            <td>
                <input style="width:145px;" type="date" id="data_fim[]" name="data_fim[]" required>
                <input style="width:60px;" type="time" id="hora_fim[]" name="hora_fim[]" required>
            </td>
        </tr>
        <?php 
            } 
        ?>
    </table>

    <div class="">
        <center>
            <button type="button" id="agendar" name="agendar" class="btn btn-primary">Agendar Lotes</button>
        </center>
    </div>
</div>

$(document).ready(function() {
    $('#agendar').click(function() {
        $.ajax({
          type: "POST",
          url: "php/salvarLote.php",
          data: null,
          success: function(data) {
            alert(data)
          },
          error: function(request, status, error) {
            // Aqui você trata um erro que possa vir a ocorrer
            // Exemplo:
            alert("Erro: " + request.responseText);
          }
        });
    });
});

PHP saveLote.php:

<?php 
  print_r($_POST); // return array()
?>

But when submitting, I can not review the contents of these fields. What's wrong?

    
asked by anonymous 27.10.2016 / 13:07

1 answer

1

You can get data from your form using .serializeArray of jquery:

var data = $('#enviar').serializeArray();

and pass in your ajax:

$.ajax({
      type: "POST",
      url: "php/salvarLote.php",
      data: data,
      ...

But the structure it will separate may not be ideal for you, so I recommend this library: link

p>

Using it, you can do this:

var data = $('#enviar').serializeObject();

And it will be able to mount the structure according to the hierarchy you specified in HTML.

    
27.10.2016 / 13:25